简体   繁体   English

如何使用ui-router在组件驱动的体系结构中定义控制器?

[英]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.

相关问题 如何使用ui-router的角度组件? - How to use angular component with ui-router? 如何使用$ scope绑定值以在状态ui-router中定义的控制器中使用ng-model查看? - How to bind value with $scope to view with ng-model from controller that define in state ui-router? 组件:ui路由器解析将不会注入控制器,数据未定义 - Component : ui-router resolve will not inject into controller, data undefined AngularJs UI路由器控制器 - AngularJs ui-router controller 如何使用ui-router中的ui-sref将参数传递给控制器 - How to pass parameters using ui-sref in ui-router to the controller 如何使用UI-Router定义可选参数而不使用尾部斜杠? - How to define optional parameter using UI-Router without trailing slash as well? 使用ui-router-有没有办法提供控制器文件的路径? - Using ui-router - is there a way to provide a path to controller file ? 使用ocLazyLoad + ui-router在AngularJS上获取错误(未注册控制器) - Getting error on AngularJS (controller not registered) using ocLazyLoad + ui-router 使用Angular ui-router基于状态参数加载控制器 - Load Controller based on state params using Angular ui-router 如何使用AngularJs,Ui-Router和MongoDB轮询数据库并填充控制器? - How do I poll a database and populate a controller using AngularJs, Ui-Router, and MongoDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM