加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在AngularJS中的另一个控制器中使用控制器

发布时间:2020-12-17 07:28:07 所属栏目:安全 来源:网络整理
导读:为什么我不能绑定到第二个控制器内的控制器变量? div ng-app="ManagerApp" div ng-controller="MainCtrl" Main: div ng-repeat="state in states" {{state}} /div div ng-controller="InsideCtrl as inside" Inside: div ng-repeat="state in inside.states
为什么我不能绑定到第二个控制器内的控制器变量?
<div ng-app="ManagerApp">
    <div ng-controller="MainCtrl">
        Main: 
        <div ng-repeat="state in states">
            {{state}}
        </div>
        <div ng-controller="InsideCtrl as inside">
            Inside:
            <div ng-repeat="state in inside.states2">
                {{state}}
            </div>
        </div>
    </div>
</div>

var angularApp = angular.module('ManagerApp',[]);

angularApp.controller('MainCtrl',['$scope',function ($scope) {
    $scope.states = ["NY","CA","WA"];
}]);

angularApp.controller('InsideCtrl',function ($scope) {
    $scope.states2 = ["NY","WA"];
}]);

示例:https://jsfiddle.net/nukRe/135/

第二次ng-repeat不起作用.

当您使用控制器时,您应该在控制器中使用此关键字
angularApp.controller('InsideCtrl',[ function() {
    var vm = this;
    vm.states2 = ["NY","WA"];
}]);

Forked Fiddle

注意

Technically you should follow one approach at a time. Don’t mix up
this two pattern together,Either use controllerAs syntax/ Normal
controller declaration as you did for MainCtrl using $scope

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读