我有一个HTML table
,其id
设置为 ng-repeat
<div class="project" ng-repeat="project in data.projects">
<h2>
{{projectState.name}}
</h2>
<table class="table" id="{{project.name + selectedType}}">
...
</table>
...
</div>
这会按预期正确设置id
,但我需要在 ng-click 调用中使用此id
。
<button ng-click="export({{project.name + selectedType}})">
...
</button>
这会在页面加载时产生错误
Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 16 of the expression [export({{project.name + selectedType}})] starting at [{project.name + selectedType}})].
如何在 ng-click 中正确引用id
的数据绑定{{project.name + selectedType}}
?
答案 0 :(得分:1)
将project.name
和selectedType
作为export
方法的2个参数传递:
<button ng-click="export(project.name,selectedType)">
...
</button>
和export
内部方法将它们连接起来:
$scope.export = function(name, type){
var val = name + type;
// ...
}
或只是从{{
}}
export
答案 1 :(得分:0)
您无需在export()
内添加表达式。这应该使用
export(project.name,selectedType)
答案 2 :(得分:0)
angular.module('myApp',[])
.controller('myCtlrl',function($scope){
$scope.projects=[
{ id: 1, name: 'John', address: 'Highway 71'},
{ id: 2, name: 'Peter', address: 'Lowstreet 4'},
{ id: 3, name: 'Amy', address: 'Apple st 652'},
{ id: 4, name: 'Hannah', address: 'Mountain 21'},
{ id: 5, name: 'Michael', address: 'Valley 345'},
{ id: 6, name: 'Sandy', address: 'Ocean blvd 2'},
{ id: 7, name: 'Betty', address: 'Green Grass 1'},
{ id: 8, name: 'Richard', address: 'Sky st 331'},
{ id: 9, name: 'Susan', address: 'One way 98'},
{ id: 10, name: 'Vicky', address: 'Yellow Garden 2'},
{ id: 11, name: 'Ben', address: 'Park Lane 38'},
{ id: 12, name: 'William', address: 'Central st 954'},
{ id: 13, name: 'Chuck', address: 'Main Road 989'},
{ id: 14, name: 'Viola', address: 'Sideway 1633'}
];
$scope.export=function(id){
alert(id);
};
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtlrl">
<div class="project">
<table class="table" id="{{project.name + address}}" border=1>
<tr><th>ExportButton</th><th>Id</th><th>Name</th><th>Address</th></tr>
<tr ng-repeat="project in projects">
<td><button ng-click="export(project.name+project.address)">Export</button></td>
<td>{{project.id}}</td>
<td>{{project.name}}</td>
<td>{{project.address}}</td>
</tr>
</table>
</div>
</div>
&#13;
答案 3 :(得分:0)
当您使用ng-click时,这意味着您已经处于角度范围内,因此您不需要使用{{}}来调用函数或评估表达式