【问题标题】:Issue angular directives using Typescript使用 Typescript 发出角度指令
【发布时间】:2015-05-27 00:07:15
【问题描述】:

您好,我正在尝试使用 Typescript 类实现以下 angular 指令,

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'
};
});

这是模板:

   <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>

这是我的方法,

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;
            };
        }
}

这是他在 html 中的调用方式:

<modal-dialog show='modalShown' width='400px' height='60%'>
  <p>Modal Content Goes here<p>
</modal-dialog>

我遇到的问题如下: 类型 '{ show: string; 上不存在属性 'dialogStyle' }'。

类型“IAttributes”上不存在属性“width”。

“typeof ModalDialogDirective”类型的参数不能分配给“any[]”类型的参数。

【问题讨论】:

    标签: javascript angularjs angularjs-directive typescript


    【解决方案1】:

    您的链接函数应该接受扩展类型的范围。声明一个扩展 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;
            };
        }
    

    如果你想获得一些模板来开始使用 angular 和 typescript,我建议你查看 SideWaffle:http://sidewaffle.com/

    【讨论】:

    • 嗨@Hugues,但我得到属性'dialogStyle'不存在类型'{显示:字符串; }'。虽然这是我试图在 Typescript jsbin.com/aDuJIku/2/edit?html,css,js 中实现的
    • 你能帮我弄清楚这个问题到底是什么意思“'typeof ModalDialogDirective'类型的参数不能分配给'any[]'类型的参数”我已经将它注册到主应用程序作为角度.module('modalRun', [ .... ]).directive('modalDialog',ModalDialogDirective)
    • 您在 app.directive 中的第二个参数应该是一个数组吧?虽然它似乎在 js 中工作,但我会尝试将 .directive 和 .controller 中的第二个参数包装在一个数组中。
    猜你喜欢
    • 2021-02-15
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    相关资源
    最近更新 更多