gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:47:36 25 4
gpt4 key购买 nike

为什么我无法绑定(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com