0

使用以下代码:

<div ng-controller="mainCtrl">    
    <div ng-repeat="record in records" ng-controller="itemCtrl">
        <span>{{record}}</span><a ng-click="inc()">inc</a>
    </div>
    <p></p>
    <div ng-repeat="record in records2">
        <span>{{record}}</span><a ng-click="inc()">inc</a>
    </div>
</div>

var mainCtrl = function($scope){
    $scope.records = [
        { val: 1},
        { val: 2},
        { val: 3},
        ];

    $scope.records2 = [1, 2, 3];
}

var itemCtrl = function($scope) {
    $scope.inc = function() {
        $scope.record.val++;
    };
}

var itemCtrl2 = function($scope) {
    $scope.inc = function() {
        $scope.record++;
    };
}

我希望“inc”链接增加这两种类型的记录。但是,似乎 2-way 绑定仅适用于第一种类型的记录(它是一个对象,我更新了它的属性)。我已经看到一些类似问题的提及,并且得到的印象是更改实际绑定对象存在问题。真的是这样吗?如果是这样,我确实认为这是一个缺失的功能。

4

2 回答 2

2

我不确定我是否理解这个问题,但这里有一些工作代码将第一个 ng-repeat 映射到记录,第二个 ng-repeat 映射到记录2。这是你想要完成的吗?第二个 ng-repeat 附加到 mainCtrl 并且在范围上没有 inc 函数。我将第二个设置为使用 itemCtrl2 控制器。

<body ng-app="test1">
<div>
    <div ng-controller="mainCtrl">
        <div ng-repeat="record in records" ng-controller="itemCtrl">
            <span>{{record.val}}</span><a ng-click="inc()">inc</a>
        </div>
        <p></p>
        <div ng-repeat="record in records2" ng-controller="itemCtrl2">
            <span>{{record}}</span><a ng-click="inc()">inc</a>
        </div>
    </div>

    <script>
        var app = angular.module("test1", []);
        app.controller("mainCtrl", function ($scope) {
            $scope.records = [
                { val: 1 },
                { val: 2 },
                { val: 3 }
            ];

            $scope.records2 = [1, 2, 3];
        });

        app.controller("itemCtrl", function ($scope) {
            $scope.inc = function () {
                $scope.record.val++;
            };
        });

        app.controller("itemCtrl2", function ($scope) {
            $scope.inc = function () {
                $scope.record++;
            };
        });

    </script>
</div>

于 2013-07-12T13:17:28.797 回答
1

你错过了第二个 ItemController:

<div ng-controller="mainCtrl">    
<div ng-repeat="record in records" ng-controller="itemCtrl">
    <span>{{record}}</span><a ng-click="inc()">inc</a>
</div>
<p></p>
<div ng-repeat="record in records2" ng-controller="itemCtrl2">
    <span>{{record}}</span><a ng-click="inc()">inc</a>
</div>

这是工作 JSFiddle http://jsfiddle.net/alfrescian/fbLc9/

于 2013-07-12T13:22:36.793 回答