私は次のようなオブジェクトを持っています:
$scope.things = [
{
name: 'Bob!',
short_name: 'bob',
info: 'something something'
},
{
name: 'Steve',
short_name: 'steve',
info: 'something something something'
},
];
私はこのようにそれらをループし、ng-clickを追加します:
<div ng-repeat="thing in things" ng-click="addThing(thing.name, thing.short_name, thing_info" ng-class="thingClass(thing.name)">content goes here</div>
ng-click="addThing()"
基本的に値をアップ房やオブジェクトに追加します。
クリックすると、クラスが追加されます。これはselected
、多次元オブジェクトを使用していないときはname
、オブジェクト/配列の内部を探しているだけなので、問題なく機能しました(この時点では、オブジェクトだと思います...しかし、当時、それは配列でした)
これと同等の方法がわかりません...
$scope.thingClass= function(name) {
if($scope.thingSelected.indexOf(name) != -1) {
return 'selected';
}
};
...現在のオブジェクトを使用します。私はグーグルを通して見つけたここからのいくつかの答えを適応させようとしました、例えば:
$scope.teamClass = function(name) {
var found = $filter('filter')($scope.thingSelected, {id: name}, true);
if (found.length) {
return 'selected';
}
};
...しかし喜びはありません。
誰かが私を正しい方向に向けたり、少しずつ動かしたりできますか?
あなたは単にthingオブジェクトをに渡すことができますthingClass
:
... ng-class="thingClass(thing)" ...
thingClass
次のように実装します。
$scope.thingClass= function(thing) {
return $scope.thingSelected.indexOf(thing) >= 0 ? 'selected' : '';
}
そして多分あなたはこのテクニックを以下にaddThing
も適用するべきです:
... ng-click="addThing(thing)" ...
$scope.addThing = function(thing) {
if ($scope.thingSelected.indexOf(thing) < 0)
$scope.thingSelected.push(thing);
}
しかし、配列内の選択されたものを追跡する代わりにselected
、それぞれにプロパティを導入する方がはるかに簡単です。
$scope.addThing = function(thing) {
thing.selected = true;
}
$scope.thingClass= function(thing) {
return thing.selected ? 'selected' : '';
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加