From 5c6cba6f94dd6ff2f02817db971f6902fc0c431b Mon Sep 17 00:00:00 2001 From: Jeffarese Date: Wed, 1 Jun 2016 17:58:08 +0200 Subject: [PATCH] feat(directive): support class syntax in TypeScript Closes #316 When using controllerAs syntax use class syntax for directives in TypeScript. --- lib/directive/templates/_directive.ts | 65 ++++++++++++++++----------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/lib/directive/templates/_directive.ts b/lib/directive/templates/_directive.ts index e73769b..d719d44 100644 --- a/lib/directive/templates/_directive.ts +++ b/lib/directive/templates/_directive.ts @@ -1,6 +1,37 @@ /// module <%= upperCamel %> { - 'use strict'; + 'use strict';<% if (!controllerAs) { %> + interface I<%= upperCamel %>Scope extends ng.IScope { + <%= lowerCamel %>: any + }<% } %> + + function <%= upperCamel %>Directive(): ng.IDirective { + return { + restrict: 'EA', + scope: {}<% if (directiveTemplateUrl) { %>, + templateUrl: '<%= templateUrl %>/<%= hyphenName %>-directive.tpl.html'<% } else { %>, + template: '
{{<%= lowerCamel %>.name}}
'<% } %>, + replace: false,<% if (controllerAs) { %> + controllerAs: '<%= lowerCamel %>',<% } %> + controller: <% if (!controllerAs) { %>function($scope: I<%= upperCamel %>Scope): void { + $scope.<%= lowerCamel %> = {}; + $scope.<%= lowerCamel %>.name = '<%= lowerCamel %>'; + },<% } else { %><%= upperCamel %>Controller,<% } %> + link: function (scope: ng.IScope, element: JQuery, attrs: any): void { + /*jshint unused:false */ + /*eslint "no-unused-vars": [2, {"args": "none"}]*/ + } + } + }<% if (controllerAs) { %> + + export class <%= upperCamel %>Controller { + public name: string; + public static $inject: Array = []; + + constructor() { + this.name = '<%= lowerCamel %>'; + } + }<% } %> /** * @ngdoc directive @@ -11,34 +42,14 @@ module <%= upperCamel %> { * @description * * @example - - - <<%= hyphenName %>>> - - + * + * + * <<%= hyphenName %>>> + * + * * */ angular .module('<% if (parentModuleName) { %><%= parentModuleName %>.<% } %><%= moduleName %>') - .directive('<%= lowerCamel %>', <%= lowerCamel %>); - - function <%= lowerCamel %>(): ng.IDirective { - return { - restrict: 'EA', - scope: {}<% if (directiveTemplateUrl) { %>, - templateUrl: '<%= templateUrl %>/<%= hyphenName %>-directive.tpl.html'<% } else { %>, - template: '
{{<%= lowerCamel %>.name}}
'<% } %>, - replace: false,<% if (controllerAs) { %> - controllerAs: '<%= lowerCamel %>',<% } %> - controller: function (<% if (!controllerAs) { %>$scope: ng.IScope<% } %>) { - <% if (controllerAs) { %>var vm = this; - vm.name = '<%= lowerCamel %>';<% } else { %>$scope.<%= lowerCamel %> = {}; - $scope.<%= lowerCamel %>.name = '<%= lowerCamel %>';<% } %> - }, - link: function (scope: ng.IScope, element: JQuery, attrs: any) { - /*jshint unused:false */ - /*eslint "no-unused-vars": [2, {"args": "none"}]*/ - } - }; - } + .directive('<%= lowerCamel %>', <%= upperCamel %>Directive); }