很难判断pos
和definition
属性的字符串来自哪里,但是使用 Angular,您希望您的视图反映在范围内可访问的一些数据——对您的视图的更改(例如,选中一个框)应该会改变一些范围内的数据。
在这种特殊情况下,我希望在一个对象上看到pos
and ;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/