2016-09-22 38 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

對不起,我正在改變執行 –