gpt4 book ai didi

javascript - 在 AngularJS 中评估指令属性中的表达式

转载 作者:数据小太阳 更新时间:2023-10-29 04:51:20 24 4
gpt4 key购买 nike

我做了很多解决方法、搜索和研究,但我不知道如何实现我的目标。

- 问题:

  • 我有以下情况,我想避免用户可以契约(Contract)中的佣金日期重叠。当用户添加新的佣金,我们显示一个列表,其中添加了生成的佣金a ngRepeat,这个有难度,用户可以编辑日期。在契约(Contract)部分,这不是问题,因为对于 edit a契约(Contract),你必须去其他屏幕编辑它,日期不能在同一 View 中进行修改。

-我感到困惑的地方:

  • 当我编辑一个添加的佣金时,我必须将它与之前添加的另一个进行比较,所以,我想要一个列表,其中定义了所有佣金的日期,并且可以在指令中说,开发一个函数,该函数返回一个列表,其中包含所有日期,不包括我正在编辑的佣金日期。

-我希望如何解决:

  • 我想做这样的事情:

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

函数 myFunction 将遍历包含所有 addedCommissionsDates 的列表,并将其与所有日期范围进行比较,addedCommisionsDates[index] 中包含的范围除外。

目标是可以在不使用隔离范围的情况下评估属性中的表达式。

我在独立作用域方面遇到了很多问题,我最终同意这篇文章:

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-require="aFunction()",我在我的指令中使用 $parsers 达到了这个目标。所以,我现在可以执行函数了!我使用它取得了一些进展,但我想在我的指令中获得执行函数的结果,我想达到这样的效果

rangesToEval =//我的函数或表达式的结果。

查看具有 ngRepeat 的东西,我无法确定返回此函数的值在什么范围内

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

一切正常,因为我使用的是 bool 值,但是,我想使用一个列表,该列表是在属性 notincluded

中执行表达式的结果

//这个输入是ng-repeat的一部分

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

我的 Controller 中有这个功能:

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

})

关于javascript - 在 AngularJS 中评估指令属性中的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19129340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com