標籤:ng-model 相關 序號 name blog button stc .com mod
這個問題的本質是:v-repeat會產生子scope,這時你在控制器裡拿值,相當於父scope裡面取子scope的值,因為Angular.js中範圍是向上尋找的,所以取不到。
操作過程如下:
相關代碼如下:
<table> <tr> <th>序號</th><th>姓名</th><th>工資</th><th>操作</th> </tr> <tr> <td>{{$index+1}}</td> <td>{{item.name}}</td> <td><input name="salary" ng-model="salary" /></td> <td><button ng-click="myAlert()">彈出工資</button></td> </tr></table><script> QryListCtrl.$inject = [‘$scope‘, ‘$remote‘]; function QryListCtrl($scope, $remote){ $scope.myAlert = function(){ alert($scope.salary); } }</script>
解決方案:
<table> <tr> <th>序號</th><th>姓名</th><th>工資</th><th>操作</th> </tr> <tr> <td>{{$index+1}}</td> <td>{{item.name}}</td> <td><input name="salary" ng-model="$parent.salary" /></td> <td><button ng-click="myAlert()">彈出工資</button></td> </tr></table>
原理:把子scope中的值通過 $parent屬性傳遞給父scope即可。
angular中ng-repeat ng-if 中的變數的值控制器中為什麼取不到