下面的代码我用于动态地将ng-init属性添加到指令中。代码工作正常,得到了我预期的输出,但在运行时我收到一些编译错误。
angular.js:13920错误:[$ parse:syntax]语法错误:表达式[{{dashboardOptions.contentSettingsOptions.dynamicContent.enable&&& 'item = tileData [dashIndex] .tiles [$ index]'|| ''}}]
<tile ng-attr-ng-init="{{dashboardOptions.contentSettingsOptions.dynamicContent.enable ?'item=tileData[dashIndex].tiles[$index]' : '' }}"
dashboard-item="tile"
ng-repeat="tile in dashitem.TileDefinitions track by $index"
>
</tile>
我收到预期的输出
<tile ng-attr-ng-init="{{dashboardOptions.contentSettingsOptions.dynamicContent.enable ?'item=tileData[dashIndex].tiles[$index]' :'' }}" dashboard-item="tile" ng-repeat="tile in dashitem.TileDefinitions track by $index" class="ng-scope angulardashboard-item tile" ng-init="item=tileData[dashIndex].tiles[$index]"></tile>
Plunker https://plnkr.co/edit/9PBwouxYrzeKsEqtXbht?p=preview
如何避免此运行时错误?
答案 0 :(得分:1)
您想通过ng-init
尝试实现什么目标?
ng-attr
用于允许angular处理标准HTML属性。假值将使属性不显示,使得该属性与值一起显示为真值。
您可能想要这样做:
<div ng-init="item = canadd ? tileData[dashIndex].tiles[$index] : null">{{message}}</div>