首頁  >  問答  >  主體

angular.js - express+angular,使用templateURL的路徑問題

使用express建構的服務,沒有使用視圖引擎,將專案目錄設定為靜態:

app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/view'));
app.use(express.static(__dirname + '/node_modules'));

node入口檔index.js在根目錄下,目錄結構為:

/
/index.js
/public
/view
/view/index.html(首页)
/view/sidebar.html(侧边菜单)
/public/js/app.js(首页的js文件)

index.html中引入如下:

<script src="angular/angular.min.js"></script>
<script src="angular-route/angular-route.min.js"></script>
<script src="js/app.js"></script>

index.html指令如下:

<my-sidebar></my-sidebar>

app.js如下:

var app = angular.module("myBlog", ["ngRoute"]);
app.directive("mySidebar", function () {
    return  {
        restrict: "E",
        replace: true,
        templateURL: "sidebar.html",
    }
})

經過測試發現templateURL沒有起作用,請問應該如何寫這個路徑,或者如何查看這裡的templateURL到底被解析成了什麼樣子?

淡淡烟草味淡淡烟草味2693 天前681

全部回覆(3)我來回復

  • 仅有的幸福

    仅有的幸福2017-05-15 17:14:24

    已經解決,我的chrome出了點問題,原程式碼就是正確寫法。
    提供一種使用ejs視圖引擎的方法:

    app.engine('html', require('ejs').__express);
    app.set('view engine', 'html');
    
    app.get('/sidebar.html', function (req, res) {
       res.render('sidebar.html');
    })

    回覆
    0
  • 阿神

    阿神2017-05-15 17:14:24

    ../../view/sidebar.html

    回覆
    0
  • 滿天的星座

    滿天的星座2017-05-15 17:14:24

    把templateURL寫成絕對路徑 templateURL: '/view/sidebar.html'

    或是在標籤裡加上組件html模板的根路徑

    回覆
    0
  • 取消回覆