ディレクティブのコンパイルをオーバーライドしてディレクティブテンプレートをカスタマイズしましたが、コンパイル関数を実行した後、リンク関数を呼び出しません。
angular.module('app').directive('ngValidate', ['$compile', function($compile){
return {
restrict: 'A',
require: '?ngModel',
compile:function($element, $attrs, linker){
// this run
console.log('compile>>>');
// append error message
$element.append('<div></div>');
$element.bind('blur',function(){
console.log('onblur');
});
$compile($element.contents());
},
controller: function($scope, $element, $attrs){
// this run
console.log('controller>>>');
},
link: function($scope, $element, $attrs, ctrl) {
// this doesn't run
console.log('link>>>');
}
}
}]);
コンパイル後にリンクを実行する必要がある理由スコープにアクセスしたいのですが、コンパイルからスコープにアクセスできますか?
コメントに記載されているように、コンパイル関数がある場合は、ディレクティブ定義オブジェクトで個別に定義するのではなく、リンク関数を返す必要があります。
angular.module('app', []).directive('ngValidate', ['$compile', function($compile){
return {
restrict: 'E',
require: '?ngModel',
compile:function($element, $attrs, linker){
// this run
console.log('compile>>>');
// append error message
$element.append('<div></div>');
$element.bind('blur',function(){
console.log('onblur');
});
$compile($element.contents());
return function($scope, $element, $attrs, ctrl) {
// this doesn't run
console.log('link>>>');
}
},
controller: function($scope, $element, $attrs){
// this run
console.log('controller>>>');
}
}
}]);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加