ng-select 选项在与自定义指令一起使用时加倍
ng-select options are doubled when used with a custom directive
我正在尝试实现动态可配置的字段。我将从服务器获取验证规则 ng-required、ng-hidden、ng-disabled 等属性作为 json,并通过指令动态设置它们。
我有以下指令代码。它显示选择值加倍 JsBin 链接 http://jsbin.com/jiququtibo/1/edit
var app = angular.module('myapp', []);
app.directive('inputConfig', function( $compile) {
return {
require: 'ngModel',
restrict: 'A',
scope: '=',
compile: function(tElem, tAttrs){
console.log("compile 2");
tElem.removeAttr('data-input-config');
tElem.removeAttr('input-config');
tElem.attr('ng-required',true);
return {
pre: function (scope, iElement, iAttrs){
console.log('pre');
},
post: function(scope, iElement, iAttrs){
console.log("post");
$compile(tElem)(scope);
}
}
}
};
});
如何解决此问题?我应该能够动态添加指令。
要解决您的问题,您需要从 post 函数中删除以下行:
$compile(tElem)(scope);
我不清楚你为什么要在这里编译,所以我不确定这是否会有任何意想不到的副作用。
我发现以下代码的解决方案正在工作。你应该首先克隆,删除指令,准备dom并编译
app.directive('inputConfig', function( $compile) { 返回 { 要求:"?ng模型", 限制:"A", 编译:function (t, tAttrs, transclude){ var tElement = t.clone() ; tElement.removeAttr('input-config'); tElement.attr('ng-required',true); t.attr('ng-required',true); 返回函数(范围){ 首先准备 DOM t.replaceWith(tElement); 比编译 $compile(tElement)(scope); }; } } });
相关文章:
- 如何使Angular JS控制器与指令一起工作
- 错误:与指令一起使用的表达式“未定义”不可赋值
- ng-select 选项在与自定义指令一起使用时加倍
- 如何将动态html与注入角度引导popover的指令一起使用
- 如何在 AngularJS 中将自定义指令与外部 HTML 模板捆绑在一起
- replace:true 如何与元素指令一起使用
- ngHide 指令仅在页面刷新后与 ngRoute 模块一起使用
- 如何将旭日图转换为指令以与 angularjs 一起使用
- 将ng repeat与指令一起使用会导致子指令不更新
- AngularJS指令包装器与Kendo一起泄漏内存
- 如何以嵌套方式将预定义指令(ng-click)与自定义指令一起使用
- 将angularjs ng repeat指令与$scope一起使用$broadcast()/$scope$on()
- 将d3与自定义的angular指令集成在一起
- Angular指令不能与select一起工作
- Angular自定义指令不能与外部javascript插件一起工作
- 在angular.js中,检测$last的指令不能与ng-if一起工作
- 将控制器和指令的交互方式捆绑在一起
- 将两个指令组合在一起
- 将工厂中的所有ngdialog组合在一起,并从指令中调用它们
- AngularJS-将两个指令绑定/链接在一起