繁体   English   中英

lodash _.includes在角度视图中不评估为真或假

[英]lodash _.includes not evaluating to true or false in angular view

我已经设置了$rootScope._ = window._; in app.run and $scope._ = _; $rootScope._ = window._; in app.run and $scope._ = _; 在控制器中。 但是,当我尝试通过_.includes求值时,内部变量的求值很好(我可以通过检查元素看到),但是ng-disabled的求值结果不是布尔值。

<li data-ng-repeat="acLink in accordion.links">
    href="#/{{acLink.domain}}/{{acLink.id}}" uib-tooltip="{{acLink.title}}" ng-disabled="_.includes({{allowedRoles}},{{acLink.role_d}})" style="color:#4d4d4d">{{acLink.title}}</a>
</li>

<a href="#/abc/home" uib-tooltip="CBOE Home" ng-disabled="_.includes(["abc", "xyz"],'abc')" style="color:#4d4d4d" class="ng-binding" aria-disabled="false"> Home</a>

我收到以下错误:

angular.min.js:103 Error: [$parse:syntax]
 at http://localhost:63342/dashboard_core/vendor/angular/angular.min.js:6:416
    at hb.throwError (http://localhost:63342/dashboard_core/vendor/angular/angular.min.js:190:254)
    at hb.primary (http://localhost:63342/dashboard_core/vendor/angular/angular.min.js:189:477)
    at hb.unary (http://localhost:63342/dashboard_core/vendor/angular/angular.min.js:197:82)
    at hb.multiplicative (http://localhost:63342/dashboard_core/vendor/angular/angular.min.js:196:324)
    at hb.additive (http://localhost:63342/dashboard_core/vendor/angular/angular.min.js:196:182)
    at hb.relational (http://localhost:63342/dashboard_core/vendor/angular/angular.min.js:196:48)
    at hb.equality (http://localhost:63342/dashboard_core/vendor/angular/angular.min.js:195:418)
    at hb.logicalAND (http://localhost:63342/dashboard_core/vendor/angular/angular.min.js:195:294) <a href="#/{{acLink.domain}}/{{acLink.id}}" uib-tooltip="{{acLink.title}}" ng-disabled="_.includes({{allowedRoles}},{{acLink.role_d}})" style="color:#4d4d4d" class="ng-binding">

我误解了代码片段。 现在,我看到您正在生成要使用插值包括在内的那些值。 在那种情况下,Tushar建议将函数移至控制器代码而不是将lodash传递至视图是一个更好的选择。

// HTML
ng-disabled="includes(allowedRoles, acLink.role_d)"

// JS
$scope.includes = function(list, item) {
    _.includes(list, item);
}

您应该为此创建一个单独的函数。 例:

$scope.isDisabled = function(){
    return _.includes(["abc", "xyz"],'abc');
};

<a href="#/abc/home" uib-tooltip="CBOE Home" ng-disabled="isDisabled()" style="color:#4d4d4d" class="ng-binding" aria-disabled="false"> Home</a>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM