AngularJS ng-repeat body 不替换 md-button ng-click 属性中的 $index
AngularJS ng-repeat body does not substitute $index in md-button ng-click attribute
我定义了以下模板:
<div ng-app="WebApp" ng-controller="PortfolioCtrl" ng-cloak layout="row" layout-align="center center" style="margin-top: 10px">
<md-content layout="row" layout-wrap style="width: 580px">
<div flex-xs flex-gt-xs="50" layout="row" ng-repeat="property in portfolio track by property.uuid">
<md-card md-theme="default" md-theme-watch="" style="width: 280px">
<md-card-title>
<md-card-title-text>
<span class="md-headline">{{property.yield}}%</span>
<span class="md-subhead">{{property.address[0]}}</span>
<span ng-if="property.address.length > 1" class="md-subhead">{{property.address[1]}}</span>
<span ng-if="property.address.length > 2" class="md-subhead">{{property.address[2]}}</span>
</md-card-title-text>
<md-card-title-media>
<!--
<div ng-if="property.photos.length > 0" class="md-media-sm card-media">
<img src="{{property.photos[0]}}" style="width: 75px; height: 85px" />
</div>
-->
</md-card-title-media>
</md-card-title>
<div style="display: none">{{$index}}</div>
<div id="property_$index" style="display: none">{{property.uuid}}</div>
<md-card-actions layout="row" layout-align="end center">
<md-button class="md-primary" ng-click="viewProperty( $index )">VIEW</md-button>
<md-button class="md-primary" ng-click="uploadPhoto( $index )">UPLOAD PHOTOS</md-button>
</md-card-actions>
</md-card>
</div>
</md-content>
</div>
查看下面的浏览器开发人员输出,$index 规范在标记中正确替换:
<div style="display: none">{{$index}}</div>
但是在按钮的 ng-click 属性中和这个 DIV id:
<div id="property_$index" style="display: none">{{property.uuid}}</div>
,他们没有替代。如果我用 {{ 和 }} 包围这些 $index 规范,我会得到一个 angular 语法错误:
$index 跟踪也不起作用。请问是什么问题?
我不恰当地使用了 {{}}。在ng-click()
中不用,但在其他地方,比如标签属性,需要用到。
正确使用:
<md-card md-theme="default" md-theme-watch="" flex-direction="column" ng-click="viewProperty( $index )" style="width: 158px">
并且:
<div id="property_{{$index}}" style="display: none">{{property.uuid}}</div>
我定义了以下模板:
<div ng-app="WebApp" ng-controller="PortfolioCtrl" ng-cloak layout="row" layout-align="center center" style="margin-top: 10px">
<md-content layout="row" layout-wrap style="width: 580px">
<div flex-xs flex-gt-xs="50" layout="row" ng-repeat="property in portfolio track by property.uuid">
<md-card md-theme="default" md-theme-watch="" style="width: 280px">
<md-card-title>
<md-card-title-text>
<span class="md-headline">{{property.yield}}%</span>
<span class="md-subhead">{{property.address[0]}}</span>
<span ng-if="property.address.length > 1" class="md-subhead">{{property.address[1]}}</span>
<span ng-if="property.address.length > 2" class="md-subhead">{{property.address[2]}}</span>
</md-card-title-text>
<md-card-title-media>
<!--
<div ng-if="property.photos.length > 0" class="md-media-sm card-media">
<img src="{{property.photos[0]}}" style="width: 75px; height: 85px" />
</div>
-->
</md-card-title-media>
</md-card-title>
<div style="display: none">{{$index}}</div>
<div id="property_$index" style="display: none">{{property.uuid}}</div>
<md-card-actions layout="row" layout-align="end center">
<md-button class="md-primary" ng-click="viewProperty( $index )">VIEW</md-button>
<md-button class="md-primary" ng-click="uploadPhoto( $index )">UPLOAD PHOTOS</md-button>
</md-card-actions>
</md-card>
</div>
</md-content>
</div>
查看下面的浏览器开发人员输出,$index 规范在标记中正确替换:
<div style="display: none">{{$index}}</div>
但是在按钮的 ng-click 属性中和这个 DIV id:
<div id="property_$index" style="display: none">{{property.uuid}}</div>
,他们没有替代。如果我用 {{ 和 }} 包围这些 $index 规范,我会得到一个 angular 语法错误:
$index 跟踪也不起作用。请问是什么问题?
我不恰当地使用了 {{}}。在ng-click()
中不用,但在其他地方,比如标签属性,需要用到。
正确使用:
<md-card md-theme="default" md-theme-watch="" flex-direction="column" ng-click="viewProperty( $index )" style="width: 158px">
并且:
<div id="property_{{$index}}" style="display: none">{{property.uuid}}</div>