<div class = "cate" ng-repeat = "cate in categories" ng-click = "funcCall('{{cate.name}}')"></div>
上面的代码使用{{cate.name}}的值在dom中呈现,但在读入功能时显示为{{cate.name}}。
如何传递这些值并在控制器功能中读取它们?
最佳答案
删除ng-click内的括号和引号,因为angular使用占位符替换之前的内容。删除花括号和引号使之有效,因此在评估代码时,它将查找具有属性cate
的变量name
并将其传递
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.categories=[
{name:"test"},
{name:"test2"},
{name:"test3"}
];
$scope.funcCall = function (vm){
alert(vm);
};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="MyCtrl">
<div class = "cate" ng-repeat = "cate in categories" ng-click = "funcCall(cate.name)">{{cate.name}}</div>
</div>
</div>