【发布时间】:2017-02-11 21:01:43
【问题描述】:
我希望在我自己的指令中使用 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);。这里没有 apply 方法。
【问题讨论】:
-
您想要的最终结果到底是什么? sn-p 您发现它非常hacky,并且可能有一种更清洁的方式来做您想做的事情。上面的代码正在深入挖掘底层框架,对其进行假设,并尝试将其完全编织成其他东西。
-
基本上我想创建我自己的我的 if 指令。我的指令将与角度服务商交谈以确定结果。我认为创建一个围绕 ngif 指令的包装器会更容易和未来证明。
标签: angularjs typescript angularjs-directive