2017-06-22 67 views
1

我使用的是角1.6.2和角材1.1.4。这里是我使用$ mdDialog组件:

class CommentReplySettingsController { 
    /** @ngInject */ 
    constructor($mdDialog, $log) { 
    this.$mdDialog = $mdDialog; 
    $log.log(this.settingType); 
    } 

    hideDialog() { 
    this.$mdDialog.hide(); 
    } 

    cancelDialog() { 
    this.$mdDialog.cancel(); 
    } 
} 

export const commentReplySettings = { 
    template: require('./comment-reply-settings.html'), 
    bindings: { 
    settingType: '<' 
    }, 
    controller: CommentReplySettingsController 
}; 

以上被转换成一个组件是这样的:

import angular from 'angular'; 

import {commentReplySettings} from './comment-reply-settings'; 

export const commentReplySettingsModule = 'commentReplySettings'; 

angular 
    .module(commentReplySettingsModule, []) 
    .component('app.user.commentReplySettings', commentReplySettings); 

这里的另一个组件的控制器功能在里面我是用里面的上述成分$ mdDialog:

showCommentReplySettingDialog(ev) { 
    this.settingType = 'global'; 
    this.$mdDialog.show({ 
     template: '<app.user.comment-reply-settings class="md-dialog-container" setting-type="$ctrl.settingType"></app.user.comment-reply-settings>', 
     parent: angular.element(this.$document.body), 
     autoWrap: false, 
     targetEvent: ev, 
     clickOutsideToClose: true, 
     fullscreen: true 
    }); 
    } 

的问题是,this.settingTypeCommen tReplySettingsController始终未定义。如何让这个工作?

编辑:如果我在CommentReplySettingsController使用settingType: '@'setting-type="global" showCommentReplySettingDialog以上功能,settingType值绑定设置正确。看起来像$ ctrl使用内部模板时变得不确定。任何原因?

回答

1

问题是$ctrl是内部$scope引用,因此,当我使用scope: this.$scope,它工作完全正常:

const _this = this; 
this.settingType = 'global'; 
this.$mdDialog.show({ 
    template: '<app.user.comment-reply-settings class="md-dialog-container" setting-type="$ctrl.settingType"></app.user.comment-reply-settings>', 
    parent: angular.element(this.$document.body), 
    autoWrap: false, 
    scope: _this.$scope, 
    targetEvent: ev, 
    clickOutsideToClose: true, 
    fullscreen: true 
}); 

但还是通过$scope需要似乎有点荒谬。希望有没有其他解决方案$scope