Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs-仅调用多个子客户/多个控制器中的一个_Angularjs_Subcontroller - Fatal编程技术网

Angularjs-仅调用多个子客户/多个控制器中的一个

Angularjs-仅调用多个子客户/多个控制器中的一个,angularjs,subcontroller,Angularjs,Subcontroller,我有一个索引页,其中定义了两个控制器。我希望始终调用一个主控制器(应始终呈现),而另一个仅针对特定的子URL调用调用。我应该让一个嵌套在另一个中,还是让它们彼此独立?我无权更改路线或任何东西,只有管制员。 现在,当我使用前面提到的模板(HTML)时,它调用/呈现两个控制器,即使url是say/index Only for /index/subPage, I want both controllers to be rendering. /index /index/subPage HTML:

我有一个索引页,其中定义了两个控制器。我希望始终调用一个主控制器(应始终呈现),而另一个仅针对特定的子URL调用调用。我应该让一个嵌套在另一个中,还是让它们彼此独立?我无权更改路线或任何东西,只有管制员。 现在,当我使用前面提到的模板(HTML)时,它调用/呈现两个控制器,即使url是say/index

Only for /index/subPage, I want both controllers to be rendering. 

/index
/index/subPage
HTML:

<div ng-controller="MainCtl" ng-init=initMain()>        
    <p> Within ctller2 {{results}} </p>
</div>


<div ng-controller="Ctller2">       <!-- should not be displayed unless /subPage/mainUrl is rendering -->
    <p> Within ctller2 {{results}} </p>
</div>

我做错了什么?我是Angular.js的新手

如果,您可以使用
ng有条件地启动控制器。所以你可以试试这样的东西:

<body ng-controller="MainCtrl">

    <div ng-controller="ctrl1">{{hello}}</div>
    <div ng-controller="ctrl2" ng-if="showCtrl2">{{hello}}</div>

</body>

但是它有点不成熟,对于一个更大的项目来说,一个更健壮的解决方案需要使用像
ui.router

这样的东西,谢谢,现在就可以了。完成后将重新访问ui.router,并且有能力重新访问:)
<body ng-controller="MainCtrl">

    <div ng-controller="ctrl1">{{hello}}</div>
    <div ng-controller="ctrl2" ng-if="showCtrl2">{{hello}}</div>

</body>
var app = angular.module('plunker', []);

app.config(function($locationProvider){
    $locationProvider.html5Mode(true); 
});

app.controller('MainCtrl', function($scope, $location) {
  $scope.showCtrl2 = ($location.path() === 'my path');
});

app.controller('ctrl1', function($scope){
  $scope.hello = 'ctrl1 says hello';
});

app.controller('ctrl2', function($scope){
  $scope.hello = 'ctrl2 says hello';
});