我試圖建立一個指令,將做到以下幾點:
- 添加其他指令元素(ngSwipeRight例如)
- 添加一些自定義行爲的新指示。
一個例子是:mySwipeBack
會增加ngSwipeRight
當用戶刷過的元素,我會做history.back()
。
我想是這樣的:
.directive('swipe', function($compile){
return {
restrict: 'A',
compile: function(el){
// I removed all the actual logic for demo purposes
// here I would add ng-swipe-right plus a handler
el.removeAttr('swipe');
var fn = $compile(el);
return function (scope) {
fn(scope);
};
}
}
});
但我遇到了一個問題與下面的標記:
<div ng-if='true'>
<h1 swipe>OUTSIDE
<div ng-if="true">INSIDE</div>
</h1>
</div>
「內部」的文字不會被渲染。 你可以看到在這個jsbin中的行爲:http://jsbin.com/tokofevuga/edit?html,js,output
如果我刪除第一個ng-if,它的工作原理如下。
有沒有人知道背後的原因是什麼 - 如果我能使它工作?
或者如果有人對如何實現我上面所述的另一個想法?
以'NG-show'作品更換任何'NG-if's的。不知道爲什麼會發生。 – koox00
是的,但我需要ng-if :) – sirrocco
@sirrocco參考指令的'compile'函數中的代碼:爲什麼您要手動編譯元素?如果刪除編譯函數中的最後三行只有'el.removeAttr('swipe');'它按預期工作 –