2016-09-22 12 views
0

ダイアログ内にコンポーネントをロードしたいので、$ scopeと依存関係注入を使って "古い"スタイルで行いました。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

これは試しましたが、動作しません:/ inject $ scope、それを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

申し訳ありません私の悪い私は実装を変更していました –

関連する問題