2013-10-02 34 views
7

我做了很多解決方法,搜索和研究,但我無法確定如何實現我的目標。評估AngularJS中某個指令的屬性表達式

- 問題:

  • 我有以下情況,我想避免用戶在合約可以 重疊佣金日期。當用戶添加一個新的 佣金時,我們顯示一個列表,其中添加了一個帶有 的ngRepeat生成的佣金列表,這有助於用戶編輯日期。 在合同部分,這不是問題,因爲編輯合同 ,您必須轉到其他屏幕並進行編輯,日期不能在相同視圖中修改爲 。

-where我會感到困惑:

  • 當我編輯加入一個委員會,我必須把它與其他比較,以前有什麼補充,所以,我想有一個列表,並定義佣金的所有日期,並且可以在指令中聲明開票函數返回一個列表,其中包含所有日期,但不包括我正在編輯的佣金的日期。

- 如何,我希望解決這個問題:

  • 我想要做這樣的事情:

<input type="text" name="newComission_dateFrom" ng-model="newCommission.from" notincluded=myFunction({{$index}})/>

和功能myFunction的,遍歷整個列表包含所有addedCommissionsDates,並將它與日期的所有範圍進行比較,除了包含在addedCom中的範圍misionsDates [指數]。

目標是可以在不使用隔離範圍的情況下評估屬性中的表達式。

我有很多的孤立範圍的問題,我處理完這個帖子同意:

When writing a directive, how do I decide if a need no new scope, a new child scope, or a new isolate scope?


編輯 我一直在尋找如何被實施ngRequire,因爲ngRequire可以接受NG-需要= 「機能缺失()」,我在指令達到這個目標,使用$解析器。 那麼,我現在可以執行一個函數! 我用這個做了一些改進,但是我想要在我的指令中執行函數的結果,我想達到這樣的效果

rangesToEval = //我的函數或表達式的結果。

尋找有ngRepeat的事情,我不能在什麼範圍內數字是返回這個函數值

if(attrs.notincluded) { 
      var notIncludedValidator = function(value) { 
      ngModelCtrl.$setValidity('notincluded', !attrs.notincluded); 
      return value; 
     }; 
    } 

一切運作良好,因爲我使用的是一個布爾值,但是,我想用那就是在屬性執行expresion的結果的列表notincluded

//這輸入是NG-重複

<input type="text" ng-model="commission.date" ng-required="true" notincluded="filterDatesFn({{$index}})" /> 
的一部分

我具備的功能在我控制器

$scope.filterDatesFn = function(index) { 
     // in this list, we will add the dates of the defined commissions 
     if($scope.addedCommisionsDate) { 
      var listToEvalue= []; 
      for (var i = 0; i < $scope.addedCommisionsDate.length; i++) { 
       if(i != index) { 
        listToEvalue.push($scope.addedCommisionsDate[i]); 
       } 
      } 
      return listToEvalue; 
     } 
    }; 

所以我的下一個目標是可以更改爲:

if(attrs.notincluded) { 
      var notIncludedValidator = function(value) { 
      --put the listToEvalue here and do my comparations and validate or invalidate the form in base of the comparation That I do here. 
      return value; 
     }; 
    } 

我將張貼這項研究的進展。

我會很感激是有人可以幫助或分享任何想法

再見!

回答

1

您是否嘗試過在您的指令中傳遞像'&'參數這樣的函數?

那樣:

App.directive('myDirective', function(){ 
return { 
    scope: { 
     myFc: '&'//if the name of parameter is the same in html if not use '&nameOfParameter' 
    }, 

    link: function($scope, $el, $attr){ 
     $el.bind('click', function(value){ 
      $scope.myFc(value) 
     }) 
    } 
} 

})