[英]How to define a controller in Component Driven Architecture using ui-router?
How to define a controller in component driven architecture using Ui-Router? 如何使用Ui-Router在组件驱动的体系结构中定义控制器?
States are included in App.js and Controllers are defined in individual component folders. 状态包含在App.js中,而控制器在单个组件文件夹中定义。
$stateProvider.state('route1', {
url:'/route1',
views: {
"ViewA": {
templateUrl: 'View.html',
controller: 'ViewController'
}
}
})
(function(myApp) {
var ViewController = function($scope) {
$scope.message='AngularJS'
};
ViewController.$inject = ['$scope','$http'];
myApp.controller('ViewController', ViewController);}(angular.module('myApp')));
<div ng-controller="ViewController" ><label> {{message}}</label>
If you mean by "Component Driven Architecture" the fact that your application has independent modules in individual component folders, 如果您的意思是“组件驱动的体系结构”,即您的应用在各个组件文件夹中具有独立的模块,
1. Create your component modules in their respective folders and attach the controllers to them 1.在各自的文件夹中创建组件模块,然后将控制器附加到它们
You can either do this if both the module and the controller is in the same file: 如果模块和控制器都在同一文件中,则可以执行以下任一操作:
angular.module('myComponent', []).controller(function($scope) {
//Code here
});
Or you can get the reference to your module like this if the controller is in another file: 或者,如果控制器在另一个文件中,则可以像这样获取对模块的引用:
angular.module('myComponent').controller(function($scope) {
//Code here
});
2. Inject your components in to your main module: 2.将组件插入主模块:
angular.module('myApp', ['myComponent'])
This way your component controllers become available in your main module and $stateProvider
configuration. 这样,您的组件控制器就可以在主模块和
$stateProvider
配置中使用。
Also note that you don't have to use ng-controller
in your View.html
. 另外请注意,您不必在
View.html
使用ng-controller
。 That is automatically done when you say controller: 'ViewController'
when changing state. 当您说
controller: 'ViewController'
更改状态时为controller: 'ViewController'
时,将自动完成此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.