将json字符串作为参数传递给ng-click

时间:2015-02-11 06:43:04

标签: angularjs angularjs-directive

我想将JSON字符串传递给ng-click

这是JSON字符串:

{"id":0,"parentID":0,"SubMenuItems":[],"imageName":"Icon.png","moduleName":"No Menu"}

HTML:

     <!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@*" data-semver="1.4.0-beta.4" src="https://code.angularjs.org/1.4.0-beta.4/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="app" ng-controller="appCtrl">
    <h1>Hello Plunker!</h1>
    <button ng-click="go({
      "id": 0,
      "parentID": 0,
      "SubMenuItems": [],
      "imageName": "Icon.png",
      "moduleName": "No Menu"
    })">GOOOOOOOOOOOOOO!!!!!!!!!!</button>
  </body>

</html>

JS: //代码在这里

var app = angular.module('app', []);
app.controller('appCtrl', ['$scope',
  function($scope) {

    $scope.go = function(parm) {
      alert('hi');
    };
  }
]);

PLUNKER

1 个答案:

答案 0 :(得分:4)

有两个问题。首先,您需要在某个元素上声明ngController指令ng-controller="appCtrl"。第二个是你必须在引号中使用ngClick属性,然后将没有引号的对象传递给go函数。 Angular会明白你正在传递和反对:

<body ng-app="app" ng-controller="appCtrl">
    <h1>Hello Plunker!</h1>
    <button ng-click='go({
      "id": 0,
      "parentID": 0,
      "SubMenuItems": [],
      "imageName": "Icon.png",
      "moduleName": "No Menu"
    })'>GOOOOOOOOOOOOOO!!!!!!!!!!</button>
</body>

演示: http://plnkr.co/edit/8WuuhbCaZBom05ep576K?p=preview