ng-click中的表达式语法

时间:2014-01-17 21:00:24

标签: javascript json angularjs

我有两个变量wkidx和dyidx

我正在尝试使用angular ui bootstrap指令在同一页面上创建多个可折叠元素,并且我有以下元素

<a 
    href=""
    class=""
    ng-click="isCollapsed{{wkidx}}{{dyidx}}  = !isCollapsed{{wkidx}}{{dyidx}}">
    Blah
</a>

现在这会将我想要的内容输出到dom(即它将变量值附加到iscollapsed)但是我得到了类似的语法错误

Syntax Error
error in component $parse
Syntax Error: Token 'wkidx' is at column {2} of the expression [{3}] starting at [{4}].

我尝试阅读错误页面链接到的页面,但它并没有让我更清楚。我尝试了很多变化,他们要么不给出错误但不附加值或者他们追加值但是抛出语法错误

谢谢你的帮助是我在,但我可能因为明显的原因而得到错误

HTML

<div
    ng-repeat="session in day.sessions"
    ng-init="ssnidx = $index">

    <a
    href=""
    class=""
    ng-click="setCollapsed(!isCollapsed(wkidx, dyidx), wkidx, dyidx)">
    </a>

    <div
        collapse="I DONT KNOW WHAT SHOULD GO HERE">
            <p>hello from the collapsed div</p>
    </div>
</div>

app.js

$scope.setCollapsed = function(value, wkidx, dyidx) {

    };

$scope.isCollapsed = function(wkidx, dyidx) {
   if (isCollapsed + wkidx + dyidx) {
       return true;
    } else {
      return false;
     }
  };

1 个答案:

答案 0 :(得分:3)

这不是ng-click的有效表达式。您最好在$scope中创建方法:

setCollapsed(value, wkidx, dyidx)isCollapsed(wkidx, dyidx)

function MyController($scope) {
   $scope.setCollapsed = function(value, wkidx, dyidx) {
       //...
   };
   $scope.isCollapsed = function(wkidx, dyidx) {
       //...
   };
}

<a 
    href=""
    class=""
    ng-click="setCollapsed(!isCollapsed(wkidx, dyidx), wkidx, dyidx)>
    Blah
</a>

假设wkidxdyidx已转移到$scope


回答您的最新问题:

<div
    ng-repeat="session in day.sessions"
    ng-init="ssnidx = $index">

    <a
    href=""
    class=""
    ng-click="setCollapsed(!isCollapsed(wkidx, dyidx), wkidx, dyidx)">
    </a>

    <div
        ng-show="isCollaspsed(wkidx, dyidx)">
            <p>hello from the collapsed div</p>
    </div>
</div>