I'm trying to convert my directive to use TypeScript
This is what I have so far
'use strict';
module app.dashboard {
export class Safes implements ng.IDirective {
static $inject = ['$log', '$http', 'storeFactory'];
static instance($log, $http, storeFactory) {
return new Safes($log, $http, storeFactory);
}
templateUrl = '/app/shared/mocks/table.html';
restrict :string = 'A';
scope: any = {
title: '=',
rows: '='
};
link: (scope, element, attrs) => void;
constructor(private $log, public $http: ng.IHttpProvider) {
this.link = this._link.bind(this);
}
_link(scope, element, attrs) {
this.storeFactory.getSafes().then(success, failure);
function success(response) {
scope.safes = response.data.safes;
localStorage.setItem('safeCount', scope.safes.length);
this.$http.get('/app/dashboard/safes/safes.html', { cache: this.$templateCache }).success(function (tplContent) {
element.replaceWith(this.$compile(tplContent)(scope));
});
}
}
}
angular
.module('app')
.directive('safes', Safes.instance);
}
When I get into my success
function it says Cannot read property '$http' of undefined
What am I doing incorrectly to not access $http
?
When I get into my success function it says Cannot read property '$http' of undefined
This is because you used function
:
function success(response) {
You probably meant to use ()=>
(arrow function) which would preserve this
context for you.
More : https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.