首页 > 解决方案 > ng-repeat中的ng-click中的AngularJS问题,用单引号传递字符串变量

问题描述

我在 angularjs 中遇到问题,我需要用单引号传递 {{item.Key}},item.Key 的值例如是在 ng-repeat 中设置的“蓝色”,因此 selectMoreFacets( ) 功能。第一次加载页面时,只有搜索栏可见,用户在搜索中输入关键字,按回车键,然后运行以下代码 ng-repeat。第一次加载页面时,我在控制台中收到以下错误,因为这可能是

{{'\''+item.Key+'\''}} 

但是点击搜索后,页面加载正常,如果我检查元素

<a ng-click="selectMoreFacets('by_Options', optionShowMore?'more':'less', 'Color Temp')"
   class="ng-binding">more...</a>

这看起来很好,当我点击<a标签时应该可以工作。但它不起作用,当我点击

 <a tag , selectMoreFacets()

函数没有被调用,也没有抛出错误。

<div ng-repeat="item in aggsOptions | limitTo:100:3" ng-hide="isLoading">
    <h4><u>{{item.Key}}</u></h4>
    <ul class="category-list">
        <li ng-repeat="subItem in item.Aggregations.Values.Items" ng-hide="isLoading">
            <span ng-class="{'label-primary label': isActive_Options(item.Key,subItem.Key)}">
                <a ng-click="toggleFilters_Options(item.Key,subItem.Key)">{{subItem.Key}}</a>
            </span><small class="pull-right">({{subItem.DocCount | number : fractionSize}})</small>
        </li>
    </ul>
    <a ng-click="selectMoreFacets('by_Options', optionShowMore?'more':'less', {{'\''+item.Key+'\''}})">{{optionShowMore?"more...":"less"}}</a>
</div>

控制台窗口中的错误。

错误:[$parse:syntax] http://errors.angularjs.org/1.4.8/$parse/syntax?p0=%7B&p1=invalid%20key&p2=63&p3=selectMoreFacets(by_Options %2C%20optionShowMore%3F'more' %3A'less'%2C%20%7B%7B'%5C''%2Bitem.Key%2B'%5C''%7D%7D&p4=%7B'%5C''%2Bitem.Key%2B'%5C ''%7D%7D>

at http://localhost:53694/Scripts/angular.min.js:6:416
at s.throwError (http://localhost:53694/Scripts/angular.min.js:210:32)
at s.object (http://localhost:53694/Scripts/angular.min.js:209:327)
at s.primary (http://localhost:53694/Scripts/angular.min.js:206:335)
at s.unary (http://localhost:53694/Scripts/angular.min.js:206:174)
at s.multiplicative (http://localhost:53694/Scripts/angular.min.js:205:434)
at s.additive (http://localhost:53694/Scripts/angular.min.js:205:261)
at s.relational (http://localhost:53694/Scripts/angular.min.js:205:96)
at s.equality (http://localhost:53694/Scripts/angular.min.js:204:425)
at s.logicalAND (http://localhost:53694/Scripts/angular.min.js:204:278) <a ng- click="selectMoreFacets('by_Options', optionShowMore?'more':'less', {{'\''+item.Key+'\''}})">```

标签: angularjs

解决方案


不需要括号和引号。只要通过item.Key

<a ng-click="selectMoreFacets('by_Options', optionShowMore?'more':'less', item.Key)">{{optionShowMore?"more...":"less"}}</a>



推荐阅读