用于多选的AngularJS指令
AngularJS directive for a multi-select
我在填充多选时遇到问题。我正在使用此版本的多选http://davidstutz.github.io/bootstrap-multiselect/
我看过这个堆栈溢出页面(如何在AngularJS中使用Bootstrap Multiselect Dropdown),但我仍然有问题。我正试图用从存储在provData中的数据库中获取的数据来填充我的多选。
这是我的html:
<div class="col-sm-8">
<select class="form-control" multiple ht-multi-select ng-model="patient.provid" ng-options="prov.id as prov.code for prov in provData">
<option value="{{prov.id}}">{{prov.code}}</option>
</select>
</div>
这是我的指示:
templates.directive('htMultiSelect', function() {
return {
replace: true,
require: 'ngModel',
scope: {},
link:function(scope, element, attrs) {
console.log($(element));
console.log('hit');
// Below setup the dropdown:
$(element).multiselect({
enableClickableOptGroups: true
});
// Below maybe some additional setup
scope.$watch(attrs.ngModel, function () {
$(element).multiselect('refresh');
});
}
};
});
我的主要问题是,我不能用provData中的数据填充多选,也不能用它来设置ng模型。任何帮助都将不胜感激。
根据Rashim的博客,这是我在Plunke中尝试的。https://rashimuddin.wordpress.com/2014/07/08/multi-select-drop-down-in-angularjs/
看看这个http://plnkr.co/edit/y8VOlgeSMOqHjFxoZU1L?p=previewenter code here
HTML
<div dropdown-multiselect="" items="FirstItems" on-close="closeAlert(val)" on-Validation="addAlert(validationMsg)" max-Selection="3" selected-items="FirstSelectedItems"></div>
角度指令为
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('AppCtrl', ["$scope", function($scope) {
$scope.status = {
isopen: false
};
$scope.FirstItems = [];
$scope.alerts = [{
type: 'danger',
msg: 'Oh snap! Change a few things up and try submitting again.'
}, {
type: 'success',
msg: 'Well done! You successfully read this important alert message.'
}];
$scope.addAlert = function(validationMsg) {
if ($scope.validateFire === true) {
$scope.alerts.push({
msg: validationMsg
});
}
$scope.validateFire = true;
};
$scope.closeAlert = function(val) {
$scope.alerts = [];
}
for (var i = 0; i <= 20; i++) {
var obj = [];
obj["Id"] = i;
obj["Name"] = "Name" + i;
obj["IsSelected"] = false;
$scope.FirstItems.push(obj);
}
$scope.FirstSelectedItems = [];
var removeItem = function(items, item) {
for (var index = 0; index < items.length; index++) {
if (item.Id == items[index].Id) {
item.IsSelected = false;
items.splice(index, 1);
break;
}
}
};
$scope.removeFirstItem = function(item) {
removeItem($scope.FirstSelectedItems, item);
};
$scope.removeSecondItem = function(item) {
removeItem($scope.SecondSelectedItems, item);
};}]);
指令是这样的
app.指令('dropdownMultiselect',function(){返回{restrict:"A",范围:{项目:"=",selectedItems:"=",maxSelection:"=",validateFire:"=",onValidation:'&',关闭:'&'},模板:"+"+"添加"+"+"+"+"+"全部"+"无"+"+"
"+"+"+"{{item.Name}}"+"+"+",控制器:函数($scope){$scope.handleClick=函数($event){$event.stopPropagation();};$scope.status={isopen:错误};$scope.status={isopen:错误};
$scope.openDropdown = function($event) {
if ($scope.items !== undefined && $scope.items.length > 0) {
if ($scope.items.length > $scope.maxSelection)
$scope.showAll = false;
else
$scope.showAll = true;
for (var index = 0; index < $scope.items.length; index++) {
$scope.items[index].IsSelected = false;
}
if ($scope.selectedItems !== undefined && $scope.selectedItems.length > 0) {
for (var selectedItemIndex = 0; selectedItemIndex < $scope.selectedItems.length; selectedItemIndex++) {
for (var itemIndex = 0; itemIndex < $scope.items.length; itemIndex++) {
if ($scope.selectedItems[selectedItemIndex].Id == $scope.items[itemIndex].Id) {
$scope.items[itemIndex].IsSelected = true;
break;
}
}
}
}
}
$event.stopPropagation();
$scope.status.isopen = true;
};
$scope.selectAll = function($event) {
$scope.selectedItems = [];
angular.forEach($scope.items, function(item) {
item.IsSelected = true;
$scope.selectedItems.push(item);
});
$event.stopPropagation();
};
$scope.deselectAll = function($event) {
angular.forEach($scope.items, function(item) {
item.IsSelected = false;
});
$scope.selectedItems = [];
$event.stopPropagation();
};
$scope.selectItem = function(item) {
if (item.IsSelected === false) {
for (var index = 0; index < $scope.selectedItems.length; index++) {
if (item.Id == $scope.selectedItems[index].Id) {
item.IsSelected = false;
$scope.selectedItems.splice(index, 1);
$scope.onClose({
val: "1"
});
break;
}
}
} else {
if ($scope.selectedItems.length > $scope.maxSelection) {
item.IsSelected = false;
$scope.validationMsg = "MAX_REACHED";
$scope.validateFire = true;
$scope.onValidation({
validationMsg: "Max_Reached"
});
return;
}
$scope.selectedItems.push(item);
}
};
$scope.clickItem = function($event) {
$event.stopPropagation();
};
$scope.closeDropDown = function() {
$scope.status.isopen = false;
$event.stopPropagation();
};
}
};});`
- AngularJs指令,该指令创建内部有数据对象的新指令
- AngularJS指令只识别双向绑定类型
- AngularJS指令出错-无法读取属性'编译'的未定义
- AngularJS指令单元测试中未定义的函数
- AngularJS指令,在元素后插入HTML
- AngularJS指令部分应用的函数don'不起作用
- AngularJS指令隔离作用域
- Google Maps API OverlayView()在AngularJS指令中不起作用
- 注入angularjs指令
- AngularJS指令模板开头的注释
- AngularJS 指令的动态选项
- AngularJS指令意外地协同工作
- Angularjs指令如何检测属性中的更改
- AngularJS指令范围约束问题重复出现
- AngularJS指令监视父级大小的更改
- 将click事件绑定到AngularJS指令中的子元素
- 如何使用TypeScript绕过AngularJS指令
- 使用transclude的AngularJS指令破坏了$scope
- AngularJS指令中的动画,事件未启动
- 用于观察高度变化的AngularJS指令