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/