函数中的角度传递值

时间:2014-11-24 20:49:47

标签: javascript angularjs visual-web-developer

<div class  = "cate" ng-repeat = "cate in categories" ng-click = "funcCall('{{cate.name}}')"></div>

上面的代码在dom中显示{{cate.name}}的值,但在函数中读取时显示为{{cate.name}}。

如何传递这些值并在控制器函数中读取它们?

2 个答案:

答案 0 :(得分:2)

删除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>

答案 1 :(得分:0)

我想你必须做

  <div class  = "cate" ng-repeat = "cate in categories" ng-click = "funcCall(cate.name)"></div>