2013-07-12 58 views
0

使用下面的代碼:2路結合不是AngularJS工作重複

<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++; 
    }; 
} 

我希望「公司」鏈接來增加這兩種類型的記錄。但是,似乎雙向綁定僅適用於第一種類型的記錄(它是一個對象,我更新了它的一個屬性)。 我見過一些類似的問題,並得到了改變實際綁定對象的問題。 這是真的嗎? 如果是這樣,我相信它是一個缺失的功能。

回答

2

我不確定我是否理解這個問題,但這裏有一些工作代碼將第一個ng-repeat映射到記錄,第二個ng-repeat映射到records2。這是你想要完成的嗎?第二個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> 

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/

+0

啊... ...我傻的:) – Yaron