Hi I am trying to implement following angular directive using Typescript classes,
angular.module('mota.main', []).directive('modalDialog', function() {
return {
restrict: 'E',
scope: {
show: '='
},
replace: true, // Replace with the template below
transclude: true, // we want to insert custom content inside the directive
link: function(scope, element, attrs) {
scope.dialogStyle = {};
if (attrs.width)
scope.dialogStyle.width = attrs.width;
if (attrs.height)
scope.dialogStyle.height = attrs.height;
scope.hideModal = function() {
scope.show = false;
};
},
templateUrl = 'src/app/selection.ts'
};
});
This is the template:
<div class='ng-modal' ng-show='show'>
<div class='ng-modal-overlay' ng-click='hideModal()'></div>
<div class='ng-modal-dialog' ng-style='dialogStyle'>
<div class='ng-modal-close' ng-click='hideModal()'>X</div>
<div class='ng- modal-dialog-content' ng-transclude></div>
</div>
</div>
This is my approach ,
export class ModalDialogDirective implements ng.IDirective {
public restrict = "E";
public scope = {show: '='};
public require = ['ngModel'];
public transclude = true;
public templateUrl = 'src/app/selection.ts';
public replace = true;
public link(scope: ng.IScope, element: JQuery, attrs: ng.IAttributes)
{
scope.dialogStyle = {};
if (attrs.width){
scope.dialogStyle.width = attrs.width;
}
if (attrs.height){
scope.dialogStyle.height = attrs.height;
}
scope.hideModal = function() {
scope.show = false;
};
}
}
This his how it gets called in html :
<modal-dialog show='modalShown' width='400px' height='60%'>
<p>Modal Content Goes here<p>
</modal-dialog>
I am getting issues as: Property 'dialogStyle' does not exist on type '{ show: string; }'.
Property 'width' does not exist on type 'IAttributes'.
Argument of type 'typeof ModalDialogDirective' is not assignable to parameter of type 'any[]'.
Your link function should accept a scope of an extended type. Declare an interface that extends ng.IScope to supply your parameters, then use that interface in your link function:
interface ImyScope extends ng.IScope{
dialogStyle:any;
hideModal:()=>void;
show:boolean;
}
public link(scope: ImyScope, element: JQuery, attrs: ng.IAttributes)
{
scope.dialogStyle:any = {};
if (attrs.width){
scope.dialogStyle.width = attrs.width;
}
if (attrs.height){
scope.dialogStyle.height = attrs.height;
}
scope.hideModal = function() {
scope.show = false;
};
}
If you wish to get some templates to get started with angular and typescript, I suggest that you check SideWaffle: http://sidewaffle.com/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With