为什么我无法绑定(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"];
}]);
注意
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 forMainCtrl
using$scope
关于angularjs - 在 AngularJS 中的另一个 Controller 中使用 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30626903/