這很難說,其中來自pos
和definition
屬性字符串從何而來,但棱角分明,你希望你的意見是一些數據的反映這是在範圍訪問 - 改變你的查看(例如,選中一個框)應該更改您範圍內的一些數據。
在這種特殊情況下,我期望在對象上看到pos
和definition
;也許控制器可以訪問它們的數組。
app.controller('WordController', function($scope) {
$scope.word = 'property';
// Maybe this array came from an $http call; maybe it was injected
// into the page with `ngInit` or `$routeProvider`.
// The point is, the controller has a client-side representation of
// the data you want to show *and manipulate* in the view.
$scope.definitions = [
{ checked: false, pos: 'noun', definition: 'Something that is owned' },
{ checked: false, pos: 'noun', definition: 'A piece of real estate, such as a parcel of land' },
{ checked: false, pos: 'noun', definition: 'real estate; the business of selling houses' },
{ checked: false, pos: 'noun', definition: 'The exclusive right of possessing, enjoying and disposing of a thing' }
// ...
];
});
現在這個信息的範圍,您可以輕鬆地給它綁定在你的觀點:
<div ng-repeat='definition in definitions'>
<input type='checkbox' ng-model='definition.checked'> {{definition.definition}}
</div>
<input type='submit' ng-click='submitDefinitions()' value='Add Word'>
由於當點擊複選框直接修改每個定義的.checked
屬性時,submitDefinitions
方法在控制器上可以容易地確定選擇哪個定義:
app.controller('WordController', function($scope) {
// ...
$scope.submitDefinitions = function() {
var selectedDefinitions = $scope.definitions.filter(function(def) {
return def.checked;
});
// do something with selected definitions
};
});
下面是一個說明基本技術中的的jsfiddle :http://jsfiddle.net/BinaryMuse/cTBm4/
謝謝。簡單用例的絕佳解決方案。 – rekordboy 2015-05-07 21:21:40