0
我期待在我自己的指令中使用NGIF作爲一種包裝。角度調用NGIF內部自定義指令
我發現下面的例子是可以正常使用...
var NAME = 'yourCustomIf';
angular.module('myApp', [])
.directive(NAME, function (ngIfDirective) {
console.log(ngIfDirective)
var ngIf = ngIfDirective[0];
return {
transclude: ngIf.transclude,
priority: ngIf.priority,
terminal: ngIf.terminal,
restrict: ngIf.restrict,
link: function ($scope, $element, $attr) {
var value = $attr[NAME];
var yourCustomValue = $scope.$eval(value);
$attr.ngIf = function() {
return yourCustomValue;
};
ngIf.link.apply(ngIf, arguments);
}
};
});
的問題是我不知道如何轉換這打字稿。這是我到目前爲止...
export class MyCustomDirective implements ng.IDirective {
constructor(private ngIfDirective: ng.IDirective) {
}
transclude = this.ngIfDirective.transclude;
priority = this.ngIfDirective.priority;
terminal = this.ngIfDirective.terminal;
restrict = this.ngIfDirective.restrict;
link = (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes, ctrl: any) => {
var atrribute = attrs["custom"];
var value = scope.$eval(atrribute);
attrs["ngIf"] =() => {
return value;
};
}
}
我的問題是最後一行ngIf.link.apply(ngIf, arguments);
。這裏沒有適用的方法。
究竟什麼是你想要的結果呢? 你發現它非常黑客,並且可能有更乾淨的方式來做你想做的事情。上面的代碼正在深入挖掘底層框架,對其進行假設,並試圖將其完全融入到其他內容中。 – Enzey
基本上我想創建我自己的我的我的if指令。我的指示將與角度服務進行交談以確定結果。我認爲創建一個包含ngif指令的包裝將會更容易和更有前途。 – fml