Skip to content

Instantly share code, notes, and snippets.

@elgervb
Last active July 29, 2016 06:56
Show Gist options
  • Save elgervb/b1fd20ee8568cbfd2b0aaeacac80ade3 to your computer and use it in GitHub Desktop.
Save elgervb/b1fd20ee8568cbfd2b0aaeacac80ade3 to your computer and use it in GitHub Desktop.
TypeScript angular 1.5 directive without scope
/**
* Angular directive to search inside a list of objects
*
* @event search-select on selection. Emits the selected obj upstream to parents
*/
class SearchDirective implements ng.IDirective {
public restrict: string = "EA";
public scope: Object = {}; // always use an isolated scope
public bindToController: Object = { // bind properties to controller (not the scope)
data: '<'
};
public controller : any = class {
static $inject = ['$scope'];
public data: string;
public $scope: ng.IScope;
constructor($scope: ng.IScope) {
this.$scope = $scope;
}
select(onj: object): void {
this.$scope.$emit('search-select', obj);
this.data = '';
}
}
public controllerAs: string = "$ctrl";
public template: string = `<div class="object-filter">
<input type="text" class="field search-field" ng-model="search" />
<ul class="search-results" ng-if="search">
<li class="search-item" ng-repeat="obj in $ctrl.data | filter: search" ng-click="$ctrl.select(obj)"> {{ obj.name }} </li>
</ul>
</div>`;
public link: ng.IDirectiveLinkFn = (scope: ng.IScope, el: ng.IAugmentedJQuery, attr: ng.IAttributes) => {
// do your DOM manipulations here
};
constructor() {}
public static factory(): ng.IDirectiveFactory {
let factory: ng.IDirectiveFactory = () => {
return new SearchDirective();
}
return factory;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment