2016-09-22 43 views
0

我想在对话框中加载一个组件,我使用$ scope和dependency injection注入了“旧”风格,并且它正在工作。Angular material对话框中的组件es5 vs es6

angular 
    .module("MyApp", ["ngMaterial"]) 
    .controller('AppCtrl', function($scope, $mdDialog, $rootElement) { 
    $scope.inputText = "Hello from the Input" 

    $scope.openDialog = function() { 
     $mdDialog.show({ 
     template: '<test text="inputText"></test>', 
     clickOutsideToClose: true, 
     parent: $rootElement, 
     scope: $scope, 
     preserveScope: true, 
     }); 
    }; 
    }) 
    .component('test', { 
    template: '<span>{{ $ctrl.text || "Default Text" }}</span>', 
    bindings: { 
     text: '<' 
    } 
    }); 

"old" style codepen

但是我把它改写为ES6风格,那么我想通过text绑定是不再可用。任何想法我错过了什么?

class AppCtrl{ 
    constructor($mdDialog) { 
    this.$mdDialog = $mdDialog; 
    this.inputText = "Hello from the Input"; 
    this.openDialog = this.openDialog.bind(this); 
    } 

    openDialog() { 
    this.$mdDialog.show({ 
     template: '<test text="this.inputText"></test>', 
     clickOutsideToClose: true, 
     preserveScope: true, 
    }); 
    }; 
} 

angular 
    .module("MyApp", ["ngMaterial"]) 
    .component('test', { 
    template: '<span>{{ $ctrl.text || "Default Text" }}</span>', 
    bindings: { 
     text: '<' 
    } 
    }) 
    .controller('AppCtrl',AppCtrl); 

ES6 style codepen

+0

您仍然需要传递的范围与'范围:$范围, '。相反,使用模板中的$ ctrl.inputText。 – estus

+0

尝试过,但不工作:/注入$范围,并将其传递给show(),仍然没有按预期工作 – Kossel

+0

好吧,'旧式'对我来说也不起作用,只是黑色覆盖和没有模态。无论如何,它就像这样简单,'$ rootElement'和'$ scope'应该在ES6中持久化。 – estus

回答

0

我和实施打,似乎ES6现在工作

http://codepen.io/luchaca/pen/qaRroz?editors=1011

class AppCtrl{ 
    constructor($mdDialog) { 
    this.$mdDialog = $mdDialog; 
    this.inputText = "Hello from the Input"; 
    } 

    openDialog() { 
    this.$mdDialog.show({ 
     template: '<test text="vm.inputText"></test>', 
     clickOutsideToClose: true, 
     controller:()=>this, 
     controllerAs: 'vm' 
    }); 
    }; 
}; 

angular 
    .module("MyApp", ["ngMaterial"]) 
    .component('test', { 
    template: '<span>{{ $ctrl.text }}</span>', 
    bindings: { 
     text: '<' 
    } 
    }) 
    .controller('AppCtrl',AppCtrl) 
+0

控制器不应该是一个箭头,因为它是一个构造函数。 – estus

+0

它不能正常工作你的codepen – Kossel

+0

对不起,我正在改变执行 –