Shohel
Shohel

Reputation: 3934

Pass json string as a parameter into ng-click

I want to pass JSON string into ng-click

here is the JSON string:

{"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: // Code goes here

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

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

PLUNKER

Upvotes: 5

Views: 6027

Answers (1)

dfsq
dfsq

Reputation: 193261

There are two problems. The first is that you need to declare ngController directive ng-controller="appCtrl" on some element. The second one is that you have to take ngClick attributes in quotes and then pass object without quotes into go function. Angular will understand that you are passing and object:

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

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

Upvotes: 5

Related Questions