2016-04-15 30 views
0
<div class="col-md-6 col-md-offset-3"> 
     <ui-view=""></ui-view> 
</div> 

我添加了ui路由器後下面的js代碼沒有運行。angular-ui路由器角js給定isundefined錯誤

任何人都可以看看我的代碼,並解釋可能是什麼問題?

我也檢查腳本參考,他們是正確的。頁面工作正常,直到我添加了UI路由器。

app.controller('SampleCtrl', [ 
'$scope', 
function($scope){ 
$scope.test = 'Hello world!'; 

$scope.posts = [ 
{title: 'post 1', upvotes: 5}, 
{title: 'post 2', upvotes: 2}, 
{title: 'post 3', upvotes: 15}, 
{title: 'post 4', upvotes: 9}, 
{title: 'post 5', upvotes: 4} 
]; 
app.config([ 
'$stateProvider', 
'$urlRouterProvider', 
function($stateProvider, $urlRouterProvider) { 
$urlRouterProvider.when("", "./Views/home"); 
$stateProvider 
.state('""', { 
    name: 'home', 
    url: './Views/home', 
    templateUrl: './Views/home.html', 
    controller: 'SampleCtrl' 
    }); 

$urlRouterProvider.otherwise('home'); 
}]); 

Error showing in COnsole

+1

後'確保你已經添加的'UI-router'參考angular.js'&然後'.STATE( ' 「」',{'應該是變化到'.state(「」,{' –

+0

)$ scope.posts = []之後;你沒有關閉控制器,並且你立即開始app.config。是這個 - 所有的代碼,或者只是你已經放在那裏的片段? – rrd

+0

對不起,但這不是控制檯錯誤 –

回答

0

你有語法錯誤,請仔細檢查一遍你的代碼。

你的代碼應該是這樣的:

app.controller('SampleCtrl', [ 
'$scope', 
function($scope){ 
$scope.test = 'Hello world!'; 

$scope.posts = [ 
{title: 'post 1', upvotes: 5}, 
{title: 'post 2', upvotes: 2}, 
{title: 'post 3', upvotes: 15}, 
{title: 'post 4', upvotes: 9}, 
{title: 'post 5', upvotes: 4} 
]; 
}]); <-- miss this close 
...