2016-02-29 26 views
0

我已經創建了一個自定義指令,它需要一些範圍參數。我使用watchGroup傳入參數更改。這是指令的一部分:

scope.$watchGroup(['repairer', 'initial'], function() { 

      if (scope.initial) { 
      scope.Repairer = scope.initial; 

      } else { 
      scope.Repairer = scope.repairer; 
      } 

      console.log('scope.Repairer value:', scope.Repairer); 

      if (scope.Repairer) { 

      console.log('wooohoo calling scriptservice'); 

      scriptingService.getScript(scope.request).then(function(scripts) { 
       scope.scripts = scripts; 
      }); 
      } else { 
      scope.scripts = null; 
      } 
     }); 

我使用的指令,因爲在我的頁面如下:

<scripting repairer="repairer" request="getRequest(repairer,initial)" initial="initial" /> 

UPDATE:張望了一下後打我越覺得這個問題是調用getRequest方法不是watchGroup聲明。

爲什麼我現在收到此錯誤:

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! 
Watchers fired in the last 5 iterations: [[{"msg":"fn: regularInterceptedExpression","newVal":{"allocationProcess":"direct","allocationSource":"internal","brand":"popie","includeSegment":false,"relationship":"lol","ruralSearch":true,"state":"VIC"},"oldVal":{"allocationProcess":"direct","allocationSource":"internal","brand":"popie","includeSegment":false,"relationship":"lol","ruralSearch":true,"state":"VIC"}}],[{"msg":"fn: regularInterceptedExpression","newVal":{"allocationProcess":"direct","allocationSource":"internal","brand":"popie","includeSegment":false,"relationship":"lol","ruralSearch":true,"state":"VIC"},"oldVal":"<<already seen>>"}],[{"msg":"fn: regularInterceptedExpression","newVal":{"allocationProcess":"direct","allocationSource":"internal","brand":"popie","includeSegment":false,"relationship":"lol","ruralSearch":true,"state":"VIC"},"oldVal":"<<already seen>>"}],[{"msg":"fn: regularInterceptedExpression","newVal":{"allocationProcess":"direct","allocationSource":"internal","brand":"popie","includeSegment":false,"relationship":"lol","ruralSearch":true,"state":"VIC"},"oldVal":"<<already seen>>"}],[{"msg":"fn: regularInterceptedExpression","newVal":{"allocationProcess":"direct","allocationSource":"internal","brand":"popie","includeSegment":false,"relationship":"lol","ruralSearch":true,"state":"VIC"},"oldVal":"<<already seen>>"}]] 

這裏是一個plunker裁判:http://plnkr.co/edit/dSkjP1Vkvwd4fVauiFqa?p=preview

回答

1

角1.x的摘要是圍繞打造髒檢查。所以通常綁定到一個函數並不是一個好主意,因爲這樣angular將需要執行這個函數來查看雙向綁定值是否有變化。在這種情況下,getRequest()函數將成爲有問題的函數。 如果您將getRequest()的結果綁定到主控制器中的變量,然後將此變量綁定到您的指令,則應該很好。

+0

問題是結果取決於修理工。當我選擇不同的修理工時,我希望getRequest返回不同的請求。 –

+0

在這種情況下,我會建議您將'getRequest'函數作爲回調傳遞給您的指令,並在更改時使用新的repairer值調用它。大致這將看起來像這樣:http://plnkr.co/edit/cUpMl3Lj8RCMMZIdAzbY?p=preview – boyomarinov

+0

真棒老兄謝謝 –

相關問題