2016-07-31 3 views
0

いずれかの回答でダイアログを確認した後にthis.statusを変更することはできますか?

class ComponentCtrl { 
    constructor($scope, $reactive, $mdDialog) { 
     'ngInject'; 

     $reactive(this).attach($scope); 
     this.$mdDialog = $mdDialog; 

     this.status = "Click button below to set status"; 
    } 

    showDialog(event) { 
     this.$mdDialog.show(confirm).then(function() { 
      // Yes 
      console.log('I like dogs'); 
      this.status = 'I like dogs'; //doesn't work 
     }, function() { 
      // No 
      console.log('I love cats'); 
      this.status = 'I love cats'; //doesn't work 
     }); 
    } 
} 

を回避する方法は、オプションでカスタムダイアログ(https://material.angularjs.org/latest/api/service/$mdDialog)を定義することです:あなたは親コントローラのすべての変数/関数にアクセスすることができますが、それはもっとたくさんのコードが含ま上記のケースで

locals: { ParentCtrl: this }, 
bindToController: true 

クイックダイアログにngMaterialの短縮形を使用するのではなく、

+0

は何をしたい、このですか? - http://codepen.io/camden-kid/pen/JKBqYN?editors=1010#0 –

+0

@camden_kid ES6クラス構文とMeteorを使用する場合、上記は機能しません。解決策は次のとおりです。ありがとう! – srokatonie

答えて

0

ここでのトリックはthis.$bindToContextです。

詳細情報: http://www.angular-meteor.com/api/1.3.11/reactive-contexthttp://www.angular-meteor.com/tutorials/socially/angular1/handling-files-with-collectionfs - "20.15ハンドルファイル選択"

class ComponentCtrl { 
    constructor($scope, $reactive, $mdDialog) { 
     'ngInject'; 

     $reactive(this).attach($scope); 
     this.$mdDialog = $mdDialog; 

     this.status = "Click button below to set status"; 
    } 

    showDialog(event) { 
     this.$mdDialog.show(confirm).then(this.$bindToContext(() => { 
      // Yes 
      console.log('I like dogs'); 
      this.status = 'I like dogs'; //working now 
     }), this.$bindToContext(() => { 
      // No 
      console.log('I love cats'); 
      this.status = 'I love cats'; //working now 
     })); 
    } 
} 
関連する問題