angularjs - 在 AngularJS 中的另一个 Controller 中使用 Controller

标签 angularjs binding controller angularjs-controller angularjs-controlleras

为什么我无法绑定(bind)到第二个 Controller 内的 Controller 变量?

<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', ['$scope', function ($scope) {
    $scope.states2 = ["NY", "CA", "WA"];
}]);

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

第二次 ng-repeat 不起作用。

最佳答案

当您使用controllerAs时,您应该在 Controller 中使用this关键字

angularApp.controller('InsideCtrl', [ function() {
    var vm = this;
    vm.states2 = ["NY", "CA", "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

关于angularjs - 在 AngularJS 中的另一个 Controller 中使用 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30626903/

相关文章:

php - 解析错误,期望 activecollab 模型类中出现 `T_PAAMAYIM_NEKUDOTAYIM' 错误

javascript - 为什么有些C# api方法需要[HttpPost]而有些则不需要?

javascript - Angular js - 修复表格的标题

javascript - AngularJS - 获取插入dom的数据

javascript - 如何在脚本标签内使用 angular.js 变量?

java - Tapestry 中的装订图 5

javascript - 有条件地以 Angular 应用一次性绑定(bind)

css - 如何在 ng-show 显示/显示元素之前添加延迟

c# - 列表框项目源 WPF 绑定(bind)

c# - Angular 6 HttpClient Post 未命中 DotNet Core 2.2 API Controller 中的 API Post 方法