2017-03-16 95 views
3

我的应用工作正常,直到我将路由从ngRoute更改为ui-Router,以便可以在视图内使用视图的强大功能。现在视图的控制器没有被注册。我的文件夹结构是这样的:使用嵌套视图时未注册的控制器

enter image description here

的index.html

<html ng-app="myApp"> 
 
    <body> 
 
    
 
     <div ui-view></div> 
 

 
     <script src="js/app.module.js"></script> 
 
     <script src="js/components/foodCategories/foodCategoriesController.js"></script> 
 
     <!-- other controllers --> 
 
     <script src="js/app.route.js"></script> 
 
    </body> 
 
</html>

app.module.js

angular.module('myApp', ['ui.router'])

app.route.js

angular.module('myApp') 
 
    .config(['$stateProvider', '$urlRouterProvider', statesManager]) 
 

 
function statesManager($stateProvider, $urlRouterProvider) { 
 
    $urlRouterProvider.otherwise('/') 
 

 
    $stateProvider 
 
    .state('profile', { 
 
     url: '/profile', 
 
     views: { 
 
     '': { 
 
      templateUrl: '/js/components/profile.html' 
 
     }, 
 
     '[email protected]': { 
 
      templateUrl: '/js/components/foodCategories/foodCategories.html', 
 
      controller: 'FoodCategoriesController' 
 
     } 
 
     } 
 
    }) 
 
}

profile.html

<div ui-view="foodCategories"></div>

foodCategories.html

<div id="foodcategories" ng-controller="FoodCategoriesController"> 
 
<!-- other stuffs -->

foodCategoriesController.js

(function() { 
 
    angular 
 
    .module('myApp') 
 
    .controller('FoodCategoriesController', controlFunc) 
 

 
    function controlFunc ($scope) { 
 
    ....my stuffs... 
 
    } 
 
})()

我不知道为什么控制器没有被注册。控制器文件加载时,我检查在开发商工具网络,但我得到错误:[$控制器:ctrlreg]在所有控制器上。 谢谢:)

回答

1

你不需要ng-controller="FoodCategoriesController"。去掉它。

+0

你是对的!我已经在app.route.js文件中为模板定义了我的控制器。愚蠢的错误。谢谢:D –

+0

欢迎。 :)请标记为答案,如果它解决了您的问题 – Shahzad