我有component()
注入导航。当我尝试将此controller
中的component
包含在内时,我收到错误。
在nav.html内部
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Demo</a>
</div>
<div id="navbar" class="collapse navbar-collapse" ng-controller="MainMenuController">
<ul class="nav navbar-nav">
<li ng-class="{ active: isActive('') }"><a href="./">home</a></li>
<li ng-class="{ active: isActive('/skus') }"><a href="sku.html">sku</a></li>
<li ng-class="{ active: isActive('/phones') }"><a href="phone.html">phone</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
在component.js中
angular.
module('mainMenu').
component('mainMenu', {
templateUrl: 'app/core/nav/nav.html',
controller: 'MainMenuController', ['$scope', '$location',
function NavController($scope, $location) {
$scope.isActive = function (viewLocation) {
var p1 = $location.path();
var p2 = '/' + p1.split("/")[1]; // split string from '/';
if(p2=='/undefined') p2 = '';
return viewLocation === p2;
}
}
]
});
但如果我像这样编写一个单独的控制器,一切正常。我对component()
做错了什么,我是否应该将控制器包含在component()
中以获得最佳实践?
angular.
module('mainMenu').
component('mainMenu', {
templateUrl: 'app/core/nav/nav.html',
});
angular.
module('mainMenu').
controller('MainMenuController', ['$scope', '$location',
function NavController($scope, $location) {
//this.navs = Nav;
$scope.isActive = function (viewLocation) {
var p1 = $location.path();
var p2 = '/' + p1.split("/")[1]; // split string from '/';
if(p2=='/undefined') p2 = '';
return viewLocation === p2;
};
}
]);
我将component
更改为此
angular.
module('mainMenu').
component('mainMenu', {
templateUrl: 'app/core/nav/nav.html',
controller: ['$scope', '$location',
function MainMenuController($scope, $location) {
//this.navs = Nav;
$scope.isActive = function (viewLocation) {
var p1 = $location.path();
var p2 = '/' + p1.split("/")[1]; // split string from '/';
if(p2=='/undefined') p2 = '';
return viewLocation === p2;
};
}
]
});
收到错误Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq?p0=MainMenuController&p1=not%20a%20function%2C%20got%20undefined
N
修复:通过清理module
和component
标题并使其唯一,从模板中删除ng-controller
;尼尔·汉密尔顿在下面的回答解决了我原来的问题。
答案 0 :(得分:3)
添加控制器时,这是我的首选...
MainMenuController.$inject = ['$scope', '$location'];
function MainMenuController($scope, $location) {}
angular.module('menu').component('mainMenu', {
controller: MainMenuController,
controllerAs: 'MainMenuController',
templateUrl: 'app/core/nav/nav.html'
});
您尝试使用内联依赖的样式...
angular.module('menu').component('mainMenu', {
controller: ['$scope', '$location', function($scope, $location) {}],
controllerAs: 'MainMenuController',
templateUrl: 'app/core/nav/nav.html'
});
区别在于您使用控制器声明获得了ControllerAs String。您应该使用controllerAs:&#34; NameForController&#34;用于命名控制器的属性。
祝你好运!