简体   繁体   English

如何创建子控制器Angular Js

[英]How to create Child Controller Angular Js

define([], function() {
    function myCtrl($scope,$http) 
    {
        $scope.test = "Course Man";

    }
    myCtrl.$inject=['$scope','$http'];
    return myCtrl;
});

We have separate file for each controller and lazy loaded when required.. They have corresponding entry in application.js. 我们为每个控制器都有单独的文件,并在需要时进行延迟加载。.它们在application.js中具有相应的条目。

Now the problem is : 现在的问题是:

I need 2-3 child controllers all linked to a parent controller.. and all are there in a single file.. so that they can be loaded.. 我需要2-3个都链接到父控制器的子控制器。并且都在一个文件中..以便可以加载它们。


Tried : 尝试过:

  define([], function() {
        function myCtrl($scope,$http) 
        {
            $scope.test = "Course Man";

        }

       function myCtrl1($scope,$http){};

        myCtrl.$inject=['$scope','$http'];
        return myCtrl;
    });

but, dosen't seems to be working. 但是,似乎没有用。

UPDATE ---- 更新----

Parent -- 父母-

 define([], function() {
        function myCtrl($scope,$http) 
        {
            $scope.test = "Course Man";

        }
        myCtrl.$inject=['$scope','$http'];
        return myCtrl;
    });

With another controller : 与另一个控制器:

define([], function() {
    function myCtrl($scope,$http) 
    {
        $scope.test = "Course Man";

    }
    return myCtrl;
});

function myCtrl1($scope,$http){

};

This is working .. not sure they have parent child relationship or not... confused ! 这很有效..不确定他们是否有亲子关系...困惑!

You can go the other way. 您可以选择其他方式。

it is possible to extend a controller or make a single controller a mixin of multiple controllers. 可以扩展一个控制器或使一个控制器成为多个控制器的混合。

module.controller('CtrlChild', ['$scope', '$controller', function ($scope, $controller) {
    // Initialize the super class and extend it.
    angular.extend(this, $controller('CtrlParent', {$scope: $scope}));
    … Additional extensions to create a mixin.
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Angular JS的子控制器中访问父控制器数据 - How to access parent controller data inside child controller in angular js 如何在Angular JS的控制器中访问JSON的子元素 - how do I access child elements of JSON in the controller in Angular JS 父子控制器的角度JS错误 - Angular JS Error with Parent Child controller 在角度js中,如何将数据从父控制器传递到子控制器? - In angular js, How do I pass data from a parent controller to a child controller? Angular JS父控制器无法将单独的变量传递给子控制器 - Angular JS parent controller unable to pass alone variables to child controller Angular.js:在ng-repeat内的子控制器中,如何传播模型更改 - Angular.js: In child controller inside ng-repeat, how to propagate up model changes 如何在Angular中访问父控制器中的子控制器范围? - How to access Child Controller Scope in Parent Controller in Angular? 如何使用WEB API 2控制器在Angular.js和PostgreSQL之间创建连接? - How to create a connection between Angular.js and PostgreSQL using a WEB API 2 controller? Angular JS SPA:如何使用Angular Controller SPA调用Angular服务? - Angular JS SPA: How to Angular Controller SPA to call Angular service? 如何两次使用Angular JS控制器 - How to Using an Angular JS controller twice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM