sha*_*enq 5 javascript angularjs typescript angularjs-directive
您好我正在尝试使用Typescript类实现以下角度指令,
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'
};
});
Run Code Online (Sandbox Code Playgroud)
这是模板:
<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>
Run Code Online (Sandbox Code Playgroud)
这是我的方法,
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;
};
}
}
Run Code Online (Sandbox Code Playgroud)
这是他在html中调用它的方式:
<modal-dialog show='modalShown' width='400px' height='60%'>
<p>Modal Content Goes here<p>
</modal-dialog>
Run Code Online (Sandbox Code Playgroud)
我遇到问题:类型'{show:string;}上不存在属性'dialogStyle' }".
'IAttributes'类型中不存在属性'width'.
'typeof ModalDialogDirective'类型的参数不能分配给'any []'类型的参数.
您的链接函数应该接受扩展类型的范围.声明扩展ng.IScope以提供参数的接口,然后在链接函数中使用该接口:
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;
};
}
Run Code Online (Sandbox Code Playgroud)
如果你想获得一些模板来开始使用angular和typescript,我建议你检查SideWaffle:http://sidewaffle.com/
| 归档时间: |
|
| 查看次数: |
3602 次 |
| 最近记录: |