テーブルにユーザーのリストを表示しています。各行の最後の列はボタンです。ボタンをクリックすると、クリックされた行に関連するユーザーを編集できるポップアップが表示されます。各ユーザーには興味のリストがあります。ポップアップでは、ユーザーの現在の関心事を表示したい複数の選択肢があります。最初にポップアップが表示されたら、正しいデータがフェッチされますが、他の行を何度もクリックしようとすると、データは同じままです。私はこれがng-model
指令のためであることを知っていますので、この状況にはどのような回避策が理想的でしょうか?ここで複数のngモデルの回避策があります
は私の複数選択
<select id="multipleSelect"
data-placeholder="Select interests..."
chosen
multiple
ng-model="$ctrl.activeInterests"
ng-options="interest for interest in $ctrl.interests"
style="width:370px; height:100px;">
<option value=""></option>
</select>
表の最後の列からボタンをクリックすると、この関数は
this.editButtonClicked = function(tableRowUser) {
$scope.firstName = tableRowUser.firstName;
$scope.lastName = tableRowUser.lastName;
$scope.email = tableRowUser.email;
$scope.role = tableRowUser.role;
$scope.tag = tableRowUser.tag;
$scope.username = tableRowUser.username;
// Fetch the active interests
fetchInterests($scope.tag);
// Fetch the available interests the user can choose
fetchAllAvailableInterests();
// After this, make the Edit Form visible
togglePopupClass();
}
と呼ばれ、「fetchInterests」関数で私が作るされていますng-modelに再度バインドされたデータは、何も成功せずに再び空になります。
function fetchInterests(newInterests) {
self.activeInterests = []; // <- Here I make it empty
var interests = newInterests.split('|');
for (i = 0; i < interests.length; i++) {
// Trim the excess whitespace.
var tag = interests[i].replace(/^\s*/, "").replace(/\s*$/, "");
self.activeInterests.push(tag);
}
}
公式ドキュメントは、それが完全に新しいオブジェクトまたはコレクションに割り当てられている場合ng-model
のみ再レンダリングすることを言います。私もこれを試しましたが、成功しませんでした。
self'は 'fetchInterests'中を指し'は何ですか?また、なぜ 'はvalue =" "ですか? – Leguest
私はマルチ選択にAngular Chosenを使用しています。公式の例では、このように使用されています。自己が 'これ'(var self = this)を参照しています –