2014-09-21 13 views
2

我害怕「範圍內的湯」,人們在功能範圍內掛着太多的功能。所以我正在嘗試面向OO的控制器,新的控制器,並在我的控制器中使用EC5風格的getter/setter。這工作很好,但現在我想用兩種方法將我的指令的作用域綁定到我的控制器的作用域,並且它不像我期望的那樣工作。我已經創建了這個codePen來展示它。如何使用新的controllerAs語法和麪向對象的控制器獲得Angular中的雙向綁定?

http://codepen.io/anon/pen/DlfxB?editors=101

我希望這條線的工作:

scope: { pants: '='}, 
+0

我不完全掌握你想達到什麼目的,但如何對這種筆嗎? http://codepen.io/anon/pen/xzmiC – 2014-09-21 18:14:00

+0

@Bonatoc OP想要將屬性附加到指令的控制器實例,而不是直接指向作用域。 BUt最終都是n範圍。控制器別名成爲範圍上的一個屬性。 – PSL 2014-09-21 18:32:43

+0

@Botanoc,你的例子直接綁定到EC5控制器,我不想那樣。指令只應該意識到它所綁定的屬性。我試圖將幾個想法混合在一起,EC5 getter和setter(如果你看起來很接近真的很棒),控制器作爲一個對象(oo controller),然後雙重綁定到一個指令的本地範圍。 – httpete 2014-09-21 21:16:05

回答

4

如果你是在1.3 RC版,您可以使用bindToController選項,有2路的約束範圍變量可以在控制器上的約束。否則,你將不得不手動將屬性映射到控制器實例(這是一個痛苦),或者只是使用常規語法(沒有控制器)來進行2種綁定屬性。所以你可以在指令配置中使用和bindToController:true

bindToController
  • 當分離物範圍被用於組分(見上文),並且controllerAs被使用,bindToController
  • 允許組件有其屬性綁定到控制器,而不是範圍。當實例化控制器
  • 時,隔離範圍綁定的初始值已經可用。

(function(){ 
 
    
 
    var myApp = angular.module('plunker', []); 
 

 
    var helloEC5 = function(){ 
 
     this.pants = "jeans"; 
 
    }; 
 
    helloEC5.prototype = { 
 
     firstName: 'Seeya', 
 
     lastName: 'Latir', 
 
     get fullName() { 
 
      return this.firstName + ' ' + this.lastName; 
 
     }, 
 
     set fullName (name) { 
 
      var words = name.toString().split(' '); 
 
      this.firstName = words[0] || ''; 
 
      this.lastName = words[1] || ''; 
 
     } 
 
    }; 
 
    myApp.controller('HelloEC5', helloEC5); 
 
    myApp.directive('myDirective', function() { 
 
     return { 
 
      scope: { pants: '='}, 
 
      controllerAs: 'my', 
 
      controller: function(){}, 
 
      bindToController:true, 
 
      template: '<h1>{{my.pants}} - myDirective.pants = {{ my.pants }}</h1><input ng-model="my.pants">' 
 
     } 
 
    }); 
 
})();
<script data-require="[email protected]" data-semver="1.3.0-rc.1" src="https://code.angularjs.org/1.3.0-rc.1/angular.js"></script> 
 

 
    <div ng-app="plunker"> 
 
    <div ng-controller="HelloEC5 as EC5"> 
 
     <p>HelloEC5 says my name is: {{EC5.fullName}} wearing {{EC5.pants}}!</p> 
 
     <label>Pants:</label> 
 
     <input ng-model="EC5.pants" type="text" /> 
 
     <label>FirstName:</label> 
 
     <input ng-model="EC5.firstName" type="text" /> 
 
     <div my-directive="" pants="EC5.pants"></div> 
 
     <hr /> 
 
     <label>Setting HelloEC5.fullName:</label> 
 
     <input ng-model="EC5.fullName" /> 
 
    </div> 
 
    </div>

+0

PSL - 這是EC5吸氣劑和吸附劑的好用嗎?我真的很喜歡它的外觀。當語言執行此操作時,我發現人們瘋狂使用$ scope.getVar()和$ scope.setVar(),並且自定義鉤子。 – httpete 2014-09-21 21:11:05

+0

@Httpete對不起,我沒有得到你的問題。使用get set很好,如果你確定不支持舊瀏覽器。 Angular爲ngModel提供了一個getter setter版本。但它不同於ES5 getter setter .. https://docs.angularjs.org/api/ng/directive/ngModelOptions – PSL 2014-09-21 22:22:42

+0

我完全沒有把IE瀏覽器放在歷史的垃圾箱裏。我的問題是我有很多scope.soup()。角度文檔中的例子太簡單了,是大多數人的出發點。 OO控制器和控制器非常有吸引力。我只想知道,如果我需要像https://github.com/Baqend/jahcode之類的東西,或者我可以使用我剛剛佈置的東西。最受歡迎的想法.. – httpete 2014-09-21 23:34:02

相關問題