2015-05-20 45 views
0

我正在設置一個簡單的粗糙應用程序。但是,我不確定使用ui-router會發生什麼。我讀過你應該使用html標籤,所以我也試過這個。這是我的代碼。ui路由器不能在玉器中工作嗎?

app.js

app.get('/templates/:templateid',indexController.getTemplate); 

索引控制器

var indexController = { 
index: function(req, res) { 
    res.render('index'); 
}, 
getTemplate:function(res,req){ 
    res.render('templates/' + req.params.templateid); 
} 

};

module.exports = indexController;

這是我main.js

var app = angular.module('myApp',['ui.router']); 

app.config(function($stateProvider,$urlProvider){ 
    $stateProvider('home',{ 
     url:'/home', 
     templateUrl:'templates/home.jade', 
     controller: 'HomeController' 
    }) 
}) 

app.controller('HomeController',function($scope){ 

    console.log('home is here'); 
}) 

我index.jade顯示了這樣

extends layout 

block content 
    h1 Lets Get Cruddy 

    <div ui-view></div> 

我所說的NG-應用在我的佈局。這應該是相當簡單的權利?

回答

0

路由器只解釋HTML

所以

templateUrl:'templates/home.html 

那如果templates/home.jade編譯成templates/home.html

0

曼哈頓博士說,AngularJS不應該指向模板URL到編譯的HTML文件渲染一個Jade文件,但是,如果你有一個服務器端語言,比如NodeJS,你可以將視圖引擎更改爲Jade。

爲了簡單起見,我還建議使用Express創建一個NodeJS應用程序,以便您可以創建一個路徑來呈現視圖並利用Jade的簡單性。

以下添加到您的App.js文件在您的應用程序的NodeJS:

app.set('view engine', 'jade'); 

在你路線的NodeJS,您將添加這樣的事情: VAR路由器= express.Router();

// index.js 
// http://www.yourdomain.com/ is the path to this route 
router.get('/', function(req, res, next) { 
    res.render('home', { 
     title: 'Express' 
    }); 
}); 

在你的main.js文件,你將templateURL更改爲以下:

templateUrl:'/', 

玉將呈現模板,HTML,使其可讀的AngularJS。