簡體   English   中英

在AngularJS中評估指令屬性中的表達式

[英]Evaluating an expression in an attribute of a directive in AngularJS

我做了很多解決方法,搜索和研究,但我無法想象如何實現我的目標。

- 問題:

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

- 我在哪里感到困惑:

  • 當我編輯一個已添加的佣金時,我必須將其與之前添加的佣金進行比較,因此,我希望有一個列表,其中定義了佣金的所有日期,並且可以在指令中說明,開具一個函數的發票返回一個列表,其中包含所有日期,不包括我正在編輯的佣金日期。

- 我希望如何解決它:

  • 我想做這樣的事情:

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

函數myFunction將迭代一個包含所有addedCommissionsDates的列表,並將它與所有日期范圍進行比較,除了包含在addedCommisionsDates [index]中的范圍。

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

孤立的范圍我遇到了很多問題,我完成了對這篇文章的同意:

在編寫指令時,如何確定是否不需要新范圍,新子范圍或新隔離范圍?


編輯我正在尋找如何實現ngRequire,因為ngRequire可以接受ng-require =“aFunction()”,我在我的指令中使用$ parsers達到了這個目標。 所以, 我現在可以執行一個功能了! 我使用它做了一些進展,但我希望在我的指令中執行該函數的結果,我希望達到這樣的效果

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

查看具有ngRepeat的內容,我無法確定返回此函數的值的范圍

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

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

//此輸入是ng-repeat的一部分

 <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;
        };
    }

我將發布這項研究的進展。

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

回頭見!

你試過在你的指令中傳遞像'&'參數這樣的函數嗎?

像那樣:

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)
        })
    }
}

})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM