2016-02-26 54 views
0

我有,我有angularjs和NG-重複的問題掙扎沒有更新,我有被NG-重複生成的表:NG重複

<div> 
    <select ng-model="item" ng-controller="dropdown" 
     ng-change="update(item)"> 
     <option value="volvo">Volvo</option> 
     <option value="saab">Saab</option> 
    </select> 
    <table ng-controller="repeatTest"> 
     <tr> 
      <td>name</td> 
      <td>family name</td> 
      <td>score</td> 
     </tr> 
     <tr ng-repeat="item in list"> 
      <td>{{ item.name }}</td> 
      <td>{{ item.familyName }}</td> 
      <td>{{ item.score }}</td> 

     </tr> 


    </table> 


</div> 

和我的js代碼如下:

var app = angular.module("myModule", []); 

app.service('sharedService', function() { 


    this.list = []; 

}); 
app.controller('dropdown', function($scope, sharedService) { 
$scope.update = function(item) { 
    if (item == "volvo") { 
     sharedService.list = [ { 
      name : 'A', 
      familyName : 'AA', 
      score : '10' 
     }, { 
      name : 'B', 
      familyName : 'BB', 
      score : '5' 
     } ]; 

     $scope.list = sharedService.list; 
     console.log($scope.list); 
    } else { 
     sharedService.list = [ { 
      name : 'zzz', 
      familyName : 'zzz', 
      score : '100' 
     }, { 
      name : 'zz', 
      familyName : 'zz', 
      score : '70' 
     } ]; 
     $scope.list = sharedService.list; 
    } 
} 

}); 
app.controller('repeatTest', function($scope, sharedService) { 
sharedService.list = [ { 
    name : 'xxx', 
    familyName : 'xxx', 
    score : 'xxx' 
}, { 
    name : 'xx', 
    familyName : 'xx', 
    score : 'xx' 
} ]; 

$scope.list = sharedService.list; 

}); 

現在我的問題是SI在頁面加載生成的,當我改變選擇框,雖然它應該是因爲我綁定表,以列表這是在範圍定義不會再更改表和我假設每當範圍變化表也應該受到影響。誰能幫忙?

+0

和'ng-controller =「repeatTest」'定義在哪裏? –

+0

@MaciejDobrowolski對不起,我忘記了,請參閱更新 –

回答

2

對於您的示例,您不需要兩個控制器。

只需將下拉控制器分配給您的父div並刪除repeatTest控制器。事情是這樣的:

<div ng-controller="dropdown"> 
    <select ng-model="item" ng-change="update(item)"> 
    <option value="volvo">Volvo</option> 
    <option value="saab">Saab</option> 
    </select> 

    <table> 
    <tr> 
     <td>name</td> 
     <td>family name</td> 
     <td>score</td> 
    </tr> 
    <tr ng-repeat="item in list"> 
     <td>{{ item.name }}</td> 
     <td>{{ item.familyName }}</td> 
     <td>{{ item.score }}</td> 
    </tr> 
    </table> 
</div> 

所以,如果你有同時處理選擇和表你不需要觀看列表變量在您的服務更改相同的控制器。

你可以用這種方式解決或執行事件,但我認爲這是很容易的。

這裏正確的代碼的jsbin:http://jsbin.com/sivapa/edit?html,js,console,output

+0

謝謝,但這是一個大場景的一個小例子,所以我需要的是用兩個控制器來處理這個問題,你知道我的方法爲什麼不起作用嗎? –

+0

是的,您的方法不起作用,因爲您需要在其他控制器(您的repeatTest)中觀察列表變量的變化。所以,你需要注入$範圍和添加後$ scope.list = sharedService.list這一行: '$ scope.watch(函數(){ 回報sharedService.list; },函數(的newval){$ scope.list = newVal; });' 這應該工作;) – eliagentili

+0

我已經更新了演示jsbin更好的解釋,這裏的鏈接:[http://jsbin.com/sivapa/edit?html ,js,console,output](http://jsbin.com/sivapa/edit?html,js,console,output) – eliagentili

2

您有兩個範圍,一個爲每個控制器。通過分配$ scope.list,您實際上是通過引用傳遞給每個控制器的列表。所以,你應該清除並重新創建陣列無需重新分配$scope.listsharedService.list,或者你可以:

兩個控制器上添加

$scope.sharedService = sharedService; 

,並使用ng-repeat="item in sharedService.list"相反,你會得到你的更新。

您當前的解決方案無法正常工作的原因是您每次調用update時都會創建一個新陣列。這意味着其他控制器的列表仍然保持對原始/前一個數組的引用。

避免每次創建一個新的數組的例子:

var result = []; 
if (item == "volvo") { 
    result = [ { 
     name : 'A', 
     familyName : 'AA', 
     score : '10' 
    }, { 
     name : 'B', 
     familyName : 'BB', 
     score : '5' 
    } ]; 
} else { 
    result = [ { 
     name : 'zzz', 
     familyName : 'zzz', 
     score : '100' 
    }, { 
     name : 'zz', 
     familyName : 'zz', 
     score : '70' 
    } ]; 
} 

//Clear array 
sharedService.list.splice(0,sharedService.list.length); 
//Copy values from result into existing array 
sharedService.list.push.apply(sharedService.list, result); 

請注意,這是未經測試。而且你還必須在某處執行$ scope.list的初始任務。

+0

如果@Troels是正確的,你也可以使用Array.splice解決這個問題()'替換數組的內容而不是用新數組替換數組。這會導致數組引用保持不變。 –

+0

所以你的意思是這個$ scope.list我更新引用,所以如果我想更新$ scope.list的值,我應該怎麼做? –

+0

永遠不要在sharedService外設置sharedService.list的值(使用賦值運算符=)。在sharedService中只做一次(就像你已經是這樣)。而不是創建一個新的數組,清除現有的並推入新的值。 –

2

'repeatTest'控制器無法知道服務數據已經改變......除非您告訴它注意這一點。試試這個版本控制器:

app.controller('repeatTest', function($scope, sharedService) { 

    $scope.list = sharedService.list; 

    $scope.$watch(function() { 
     return sharedService.list; 
    }, 
    function(o, n) { 
     if (o != n) $scope.list = n 
    }); 
}); 

此外,由於你的下拉控制器不直接影響repeatTest列表中,您不需要在控制器更新$ scope.list:

app.controller('dropdown', function($scope, sharedService) { 
    $scope.update = function(item) { 
    console.log(item); 
    if (item == "volvo") { 
     sharedService.list = [{ 
     name: 'A', 
     familyName: 'AA', 
     score: '10' 
     }, { 
     name: 'B', 
     familyName: 'BB', 
     score: '5' 
     }]; 
    } else { 
     sharedService.list = [{ 
     name: 'BMW', 
     familyName: 'Germany', 
     score: '100' 
     }, { 
     name: 'Frari', 
     familyName: 'America', 
     score: '100' 
     }]; 
    } 
    } 
    // Do a little initialization 
    $scope.item="volvo"; 
    $scope.update($scope.item); 
}); 

這裏是一個plunk:http://plnkr.co/edit/42snC2WyfdKfNzrIKA0o

+0

現在感謝我從我在角度學到的東西感到困惑,雖然我是初學者,當我們將變量分配給範圍時,它在任何地方都可以訪問我是對嗎?如果是,那麼該變量的任何更新應該對模塊可見!你能解釋一點嗎? –

+0

是的,但是當您致電更新時,您並未更新repeatTest中的$ scope.list。而且,由於您正在更改參考,而不是價值,它不會起作用。如果你對參考價值不夠強,請閱讀本頁:http://docstore.mik.ua/orelly/webprog/jscript/ch04_04.htm –