Javascript 有一个多ng模型解决方案

Javascript 有一个多ng模型解决方案,javascript,angularjs,angular-ngmodel,multiple-select,Javascript,Angularjs,Angular Ngmodel,Multiple Select,我在表中显示用户列表,每行的最后一列是一个按钮。单击按钮时,会出现一个弹出窗口,可以编辑与单击行相关的用户。每个用户都有一个兴趣列表。在弹出窗口中,我有一个多重选择,我想在其中显示用户的当前兴趣。当弹出窗口第一次出现时,它会获取正确的数据,但当我尝试多次单击其他行时,数据保持不变。我知道这是因为ng model指令,那么对于这种情况,什么样的解决方法才是最理想的呢 这是我的多选 <select id="multipleSelect" data-placeholder="Select

我在表中显示用户列表,每行的最后一列是一个按钮。单击按钮时,会出现一个弹出窗口,可以编辑与单击行相关的用户。每个用户都有一个兴趣列表。在弹出窗口中,我有一个多重选择,我想在其中显示用户的当前兴趣。当弹出窗口第一次出现时,它会获取正确的数据,但当我尝试多次单击其他行时,数据保持不变。我知道这是因为
ng model
指令,那么对于这种情况,什么样的解决方法才是最理想的呢

这是我的多选

<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>
在“fetchInterests”函数中,我将绑定到ng模型的数据再次变为空,但没有成功

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);
    }
}
函数fetchInterests(newInterests){
self.activeInterests=[];//很遗憾,根据您提供的代码,我无法告诉您解决方案有什么问题。

这里有一个快速演示,可以帮助您解决问题。

//代码在这里
angular.module('myApp',[])
.controller('myCtrl',['$scope',函数($scope){
$scope.allInterests=[‘汽车’、‘足球’、‘钓鱼’、‘宠物’、‘烹饪’];
$scope.users=[
{
姓名:'鲍勃',
年龄:34岁,
兴趣:[“汽车”、“足球”]
},
{
姓名:'玛丽',
年龄:33岁,
兴趣:[“宠物”、“烹饪”]
}
];
$scope.showDetails=函数(用户){
$scope.selectedUser=用户;
};
}]);

{{user.name}
{{user.age}
表现出兴趣

编辑:{{selectedUser.name}}
年龄:{{selectedUser.Age}}

所选兴趣:{{selectedUser.interests.join(',')}
什么是
self
指的是
fetchInterests
?还有为什么
有value=“”?我正在使用Angular Selected进行多重选择,在一个官方示例中,它是这样使用的。self指的是“this”(var self=this)。谢谢,我一到我的计算机就会尝试这个,我会让你知道它是否有效。
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);
    }
}