0
我想創建一個指令,基本上包裝input
。因此,我想將一些屬性/參數傳遞給指令,例如ng-model
。無法綁定到控制器沒有指示'searchBox的控制器
但是我無法正常工作,此刻當我在頁面中包含指令時,出現上述錯誤。
interface ISearchBoxDirectiveScope extends angular.IScope {
ngModel: string;
placeholderText: string;
}
class SearchBoxDirectiveController {
static $inject = ["$scope"];
constructor(
private $scope: ISearchBoxDirectiveScope) {
console.log("This doesnt get hit", $scope);
}
public clearSearchTerm() {
console.log("here");
this.$scope.ngModel = "";
}
}
class SearchBoxDirective implements angular.IDirective {
restrict = "E";
replace = true;
scope = {
ngModel: "=",
placeholderText: "@"
};
templateUrl = "app/directives/searchBox/searchBox.template.html";
controller: SearchBoxDirectiveController;
controllerAs = "$ctrl";
bindToController = true;
public link: (scope: ISearchBoxDirectiveScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes) => void;
constructor() {
SearchBoxDirective.prototype.link = ($scope: ISearchBoxDirectiveScope, $element: angular.IAugmentedJQuery, attrs: ng.IAttributes) => {
console.log("SearchBoxDirective.prototype.link", $scope, attrs);
}
}
static factory(): ng.IDirectiveFactory {
const directive =() => new SearchBoxDirective();
directive.$inject = [];
return directive;
}
}
angular
.module("app.directives")
.directive("searchBox", SearchBoxDirective.factory());
和IM調用它像這樣
<search-box ng-model="$ctrl.rightSideFilter" placeholder-text="Search Vehicles"></search-box>