2017-06-22 66 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