カスタム ディレクティブ内に配置されている選択要素の値を設定および取得する方法を見つけるのを手伝ってくれませんか。
これは私が持っているものです:
<body ng-app="myApp">
<div ng-controller="MyCtrl">
<div ng-repeat="category in categories">
{{category.Name}}:
<status-selector items="statuses" ng-model="selectedStates[status.Id]"></status-selector>
</div>
</div>
</body>
カテゴリとステータスの 2 つの配列があります。各カテゴリには、独自のステータスがあります。カテゴリのステータスが選択されたら、selectedStatus に保存して、最終的に のようなものにする必要があります[{CategoryId:1,StatusId:2},{CategoryId:2,StatusId:3}]
。selectedStatus が既に初期化されている場合、対応するカテゴリの選択されたステータスを確認したい場合は、値を読み取るだけでなく、値も入力する必要があることを意味します。
myApp
.controller("MyCtrl", function($scope) {
$scope.categories = [{Id:1, Name: "Category1"}, {Id: 2, Name: "Category2"}];
$scope.statuses = [{Id: 1, Name: "Low"}, {Id: 2, Name: "Normal"}, {Id: 3, Name: "High"}]
$scope.selectedStates = {};
})
.directive('statusSelector', function() {
return {
restrict: 'E',
replace: true,
scope: { items: '=', ngModel: '='},
template: '<span><select class="select" ng-options="obj.Id as obj.Name for obj in items" ng-model="ngModel"></select></span>',
link: function(scope, element, attrs) {
}
}
});
ありがとうございました。
デモ:フィドル