Javascript angularjs-更新'acute.选择'ac change'后的'list'`
我正在使用Javascript angularjs-更新'acute.选择'ac change'后的'list'`,javascript,angularjs,Javascript,Angularjs,我正在使用acute。在我的angularjs项目中选择。我有一个数组: $scope.users = []; $scope.acuteusers =[{UserName:"john"},{UserName:"joe"},{UserName:"mary"}]; $scope.stateSelected = function(state){ console.log(state); for (var i = 0; i < $sc
acute。在我的angularjs
项目中选择。我有一个数组
:
$scope.users = [];
$scope.acuteusers =[{UserName:"john"},{UserName:"joe"},{UserName:"mary"}];
$scope.stateSelected = function(state){
console.log(state);
for (var i = 0; i < $scope.acuteusers.length; i++) {
if (state.UserName == $scope.acuteusers[i].UserName) {
$scope.acuteusers.splice(i,1);
};
};
console.log($scope.acuteusers);
}
我想从acute中取出元素。选择下拉列表,列出所选元素的所有内容。但不知何故,它将保持初始状态(没有删除任何元素),即使console.log
显示它已经被删除。如何解决这个问题?在阅读了大量关于指令(,)的独立作用域的内容后,我决定破解acute.select.js
指令
首先,我在acute.select.js
中添加了一个新的范围数据
:
.directive("acSelect", function($parse, acuteSelectService) {
var defaultSettings = acuteSelectService.getSettings();
return {
restrict: "EAC",
scope: {
"acSettings": "@",
"acOptions": "@",
"model": "=acModel",
"acChange": "&",
"keyField": "@acKey",
"acRefresh": "=",///I believe you can use this, but no documentation at all.
"acFocusWhen": "=",
"id": "@",
data:"=acuteOptions"///newly added by me.
},
replace: true,
templateUrl: defaultSettings.templatePath + "acute.select.htm",
link: function(scope, element, attrs) {
scope.initialise();
///**Then I added in $watchCollection to watch the `data` changes.
scope.$watchCollection('data', function(newVals, oldVals) {
///**I found out scope.allItems is the array to build and display the list.
///however it must follow {text,value,index} object format. So I reformat it that way.
scope.allItems = [];
for (var i = 0; i < newVals.length; i++) {
scope.allItems.push({text:newVals[i].UserName,value:newVals[i],index:i});
};
}, true);
},
现在,只要$scope.acuteuser
发生更改,它就会更改下拉列表
我仍然相信,acRefresh
可以做这项工作,但我就是不知道怎么做。如果有人能分享一个更强大的解决方案,那就太好了。这可能是您的ac-
中的问题。在拼接过程中添加$timeout有时触发$digest
需要时间。我认为拼接应该类似于$scope.acuteusers.splice(i,1);不要在迭代数组时修改它,它是一种反模式。如果在迭代数组时必须从数组中删除元素,请向后执行(使用array.len
和减量启动计数器)。@AnikIslamAbhi添加了$timeout
,结果仍然相同。它看起来像是acute。选择在它自己的范围内有一组数组。我注意到有一个acRefresh
选项。如何使用它?
.directive("acSelect", function($parse, acuteSelectService) {
var defaultSettings = acuteSelectService.getSettings();
return {
restrict: "EAC",
scope: {
"acSettings": "@",
"acOptions": "@",
"model": "=acModel",
"acChange": "&",
"keyField": "@acKey",
"acRefresh": "=",///I believe you can use this, but no documentation at all.
"acFocusWhen": "=",
"id": "@",
data:"=acuteOptions"///newly added by me.
},
replace: true,
templateUrl: defaultSettings.templatePath + "acute.select.htm",
link: function(scope, element, attrs) {
scope.initialise();
///**Then I added in $watchCollection to watch the `data` changes.
scope.$watchCollection('data', function(newVals, oldVals) {
///**I found out scope.allItems is the array to build and display the list.
///however it must follow {text,value,index} object format. So I reformat it that way.
scope.allItems = [];
for (var i = 0; i < newVals.length; i++) {
scope.allItems.push({text:newVals[i].UserName,value:newVals[i],index:i});
};
}, true);
},
<select class="ac-select stateList" ac-model="users" acute-options="acuteusers" ac-options="s.UserName for s in acuteusers"
ac-settings="{ comboMode: true, loadOnOpen: true, minWidth: '470px' }" ac-change="stateSelected(value)" >
</select>