Angularjs 从另一个指令编译指令
我目前正在将Angularjs 从另一个指令编译指令,angularjs,angular-directive,Angularjs,Angular Directive,我目前正在将ng model options指令添加到我的许多输入框中,以进行去盎司处理。我的元素如下所示: <input type="text" ng-model="search" ng-model-options="{ debounce: { 'default': 200 } }" /> ctrl.$options = {debounce:{default:300}, updateOnDefault: true}; 我已尝试执行此指令,如下所示: app.directive(
ng model options
指令添加到我的许多输入框中,以进行去盎司处理。我的元素如下所示:
<input type="text" ng-model="search" ng-model-options="{ debounce: { 'default': 200 } }" />
ctrl.$options = {debounce:{default:300}, updateOnDefault: true};
我已尝试执行此指令,如下所示:
app.directive('debounce', ['$compile', function ($compile) {
return {
restrict: 'A',
replace: false,
link: function (scope, element, attrs) {
element.attr('ng-model-options', "{ debounce: { 'default': 200 } }");
$compile(element.contents())(scope);
}
}
}]);
它似乎产生了正确的HTML,但去盎司并没有起到任何作用。我的指令有什么问题?在Angular中不太支持向自己动态添加指令。您在正确的编译轨道上,但是
ng model
指令已经在您进行编译时编译好了
由于ngModel
被记录为以优先级1运行,因此您至少需要是terminal
并以优先级2运行,以便在适当的时间链接您的ng model选项
app.directive('debounce', ['$compile', function ($compile) {
return {
restrict: 'A',
priority: 2,
terminal: true,
compile: function(tElement) {
tElement.attr('ng-model-options', "{ debounce: { 'default': 200 } }");
return function (scope, element, attrs, controllers, transclude) {
$compile(element, null, 2)(scope, {
parentBoundTranscludeFn: transclude
});
};
}
}
}]);
在Angular中,动态地向自己添加指令不太受支持。您在正确的编译轨道上,但是ng model
指令已经在您进行编译时编译好了
由于ngModel
被记录为以优先级1运行,因此您至少需要是terminal
并以优先级2运行,以便在适当的时间链接您的ng model选项
app.directive('debounce', ['$compile', function ($compile) {
return {
restrict: 'A',
priority: 2,
terminal: true,
compile: function(tElement) {
tElement.attr('ng-model-options', "{ debounce: { 'default': 200 } }");
return function (scope, element, attrs, controllers, transclude) {
$compile(element, null, 2)(scope, {
parentBoundTranscludeFn: transclude
});
};
}
}
}]);
在Angular中,动态地向自己添加指令不太受支持。您在正确的编译轨道上,但是ng model
指令已经在您进行编译时编译好了
由于ngModel
被记录为以优先级1运行,因此您至少需要是terminal
并以优先级2运行,以便在适当的时间链接您的ng model选项
app.directive('debounce', ['$compile', function ($compile) {
return {
restrict: 'A',
priority: 2,
terminal: true,
compile: function(tElement) {
tElement.attr('ng-model-options', "{ debounce: { 'default': 200 } }");
return function (scope, element, attrs, controllers, transclude) {
$compile(element, null, 2)(scope, {
parentBoundTranscludeFn: transclude
});
};
}
}
}]);
在Angular中,动态地向自己添加指令不太受支持。您在正确的编译轨道上,但是ng model
指令已经在您进行编译时编译好了
由于ngModel
被记录为以优先级1运行,因此您至少需要是terminal
并以优先级2运行,以便在适当的时间链接您的ng model选项
app.directive('debounce', ['$compile', function ($compile) {
return {
restrict: 'A',
priority: 2,
terminal: true,
compile: function(tElement) {
tElement.attr('ng-model-options', "{ debounce: { 'default': 200 } }");
return function (scope, element, attrs, controllers, transclude) {
$compile(element, null, 2)(scope, {
parentBoundTranscludeFn: transclude
});
};
}
}
}]);
要执行同样的操作,您需要添加优先级更高的指令,以避免编译其他指令&这将使terminal
选项设置为true。这将指示当该指令运行时没有其他指令,然后从该指令删除
指令属性并添加ng model options
以应用去抖动更改
要避免无限编译,必须删除debounce
属性
指令
app.directive('debounce', ['$compile', function ($compile) {
return {
restrict: 'A',
priority: 1,
terminal: true,
compile: function(element, attrs) {
//compile when scope is not linked to the DOM.
element.attr('ng-model-options', "{ debounce: { 'default': 200 } }");
element.removeAttr('debounce'); //this removal is necessary to avoid infinite compile
var compile = $compile(element);
return function (scope, element, attrs) {
var link = compile(scope);
};
}
}
}]);
要执行同样的操作,您需要添加优先级更高的指令,以避免编译其他指令&这将使terminal
选项设置为true。这将指示当该指令运行时没有其他指令,然后从该指令删除
指令属性并添加ng model options
以应用去抖动更改
要避免无限编译,必须删除debounce
属性
指令
app.directive('debounce', ['$compile', function ($compile) {
return {
restrict: 'A',
priority: 1,
terminal: true,
compile: function(element, attrs) {
//compile when scope is not linked to the DOM.
element.attr('ng-model-options', "{ debounce: { 'default': 200 } }");
element.removeAttr('debounce'); //this removal is necessary to avoid infinite compile
var compile = $compile(element);
return function (scope, element, attrs) {
var link = compile(scope);
};
}
}
}]);
要执行同样的操作,您需要添加优先级更高的指令,以避免编译其他指令&这将使terminal
选项设置为true。这将指示当该指令运行时没有其他指令,然后从该指令删除
指令属性并添加ng model options
以应用去抖动更改
要避免无限编译,必须删除debounce
属性
指令
app.directive('debounce', ['$compile', function ($compile) {
return {
restrict: 'A',
priority: 1,
terminal: true,
compile: function(element, attrs) {
//compile when scope is not linked to the DOM.
element.attr('ng-model-options', "{ debounce: { 'default': 200 } }");
element.removeAttr('debounce'); //this removal is necessary to avoid infinite compile
var compile = $compile(element);
return function (scope, element, attrs) {
var link = compile(scope);
};
}
}
}]);
要执行同样的操作,您需要添加优先级更高的指令,以避免编译其他指令&这将使terminal
选项设置为true。这将指示当该指令运行时没有其他指令,然后从该指令删除
指令属性并添加ng model options
以应用去抖动更改
要避免无限编译,必须删除debounce
属性
指令
app.directive('debounce', ['$compile', function ($compile) {
return {
restrict: 'A',
priority: 1,
terminal: true,
compile: function(element, attrs) {
//compile when scope is not linked to the DOM.
element.attr('ng-model-options', "{ debounce: { 'default': 200 } }");
element.removeAttr('debounce'); //this removal is necessary to avoid infinite compile
var compile = $compile(element);
return function (scope, element, attrs) {
var link = compile(scope);
};
}
}
}]);
实际上,您甚至不需要访问元素。您可以在ngModel控制器的$options
属性中设置选项,并设置如下所示的必要值:
<input type="text" ng-model="search" ng-model-options="{ debounce: { 'default': 200 } }" />
ctrl.$options = {debounce:{default:300}, updateOnDefault: true};
代码:
angular.module('app',[]).directive('debounce',['$timeout',
函数($timeout){
返回{
限制:“A”,
要求:'ngModel',
替换:false,
链接:函数(范围、元素、属性、ctrl){
var options=ctrl.$options | |{};
ctrl.$options=angular.extend(选项|{}{
去盎司:{
默认值:300
},
updateOnDefault:true
});
}
}
}
]).controller('测试',函数()){
this.callMe=函数(){
console.log(this.search);
}
});代码>
{{vm.search}
实际上,您甚至不需要访问元素。您可以在ngModel控制器的$options
属性中设置选项,并设置如下所示的必要值:
<input type="text" ng-model="search" ng-model-options="{ debounce: { 'default': 200 } }" />
ctrl.$options = {debounce:{default:300}, updateOnDefault: true};
代码:
angular.module('app',[]).directive('debounce',['$timeout',
函数($timeout){
返回{
限制:“A”,
要求:'ngModel',
替换:false,
链接:函数(范围、元素、属性、ctrl){
var options=ctrl.$options | |{};
ctrl.$options=angular.extend(选项|{}{
去盎司:{
默认值:300
},
updateOnDefault:true
});
}
}
}
]).controller('测试',函数()){
this.callMe=函数(){
console.log(this.search);
}
});代码>
{{vm.search}
实际上,您甚至不需要访问元素。你可以