AngularUI SELECT2的onChange方法 [英] AngularUI select2 onChange method
本文介绍了AngularUI SELECT2的onChange方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何去这样做沿着这些路线的内容:
<选择UI-SELECT2多个=真的变化=的onChange()的数据-NG-模式=select2Model>< /选择>
在哪里我的控制器,我有的onChange(select2OnChangeData)定义。
我尝试添加该
范围:{
模式:= ngModel
的onChange:与&的onChange
},
要角的UI,但改变的范围变量,打破了其他的功能。
我真的想从做切莫:
。对(变,功能(E)
块引用>解决方案好东西,我也只是这个在我的项目:
HTML
<选择数据占位符=选择资产阶级=输入xxlargeUI-SELECT2 =sourceAssetIdNG模型=sourceAssetIdNG选项= asset.id作为asset.name在资产资产>< /选择>
指令
module.directive(uiSelect2功能(){
VAR连接=功能(范围,元素,属性){
element.select2(); 范围。$腕表(attr.ngModel,功能(为newValue,属性oldValue){
的console.log(uiSelect,attr.ngModel,为newValue,属性oldValue); //给这个新选项的渲染时间
的setTimeout(函数(){
如果(newValue)以element.trigger(变);
})
});
} 返回{
链接:链接
}
});相关负责code
$范围。$表(sourceAssetId功能(为newValue,属性oldValue){
如果(newValue)以$ scope.fetchAsset();
});How do I go about doing something along these lines:
<select ui-select2 multiple="true" on-change="onChange()" data-ng-model="select2Model"></select>
where in my controller, I have the onChange(select2OnChangeData) defined.
I tried adding this
scope: { model: "=ngModel", onChange: "&onChange" },
to angular-ui, but that changed the scope variable and broke the rest of the functionality.
I'd really like to refrain from doing:
.on("change", function(e)
解决方案Good thing I did just this in my project:
HTML
<select data-placeholder="Select an asset" class="input-xxlarge" ui-select2="sourceAssetId" ng-model="sourceAssetId" ng-options="asset.id as asset.name for asset in assets"></select>
Directive
module.directive("uiSelect2", function() { var linker = function(scope, element, attr) { element.select2(); scope.$watch(attr.ngModel, function(newValue, oldValue) { console.log("uiSelect", attr.ngModel, newValue, oldValue); // Give the new options time to render setTimeout(function() { if(newValue) element.trigger("change"); }) }); } return { link: linker } });
Relevant Controller Code
$scope.$watch("sourceAssetId", function(newValue, oldValue) { if(newValue) $scope.fetchAsset(); });
这篇关于AngularUI SELECT2的onChange方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文