Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 467
Shohel Avatar asked Feb 11 '15 06:02

Shohel


1 Answers

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

like image 184
dfsq Avatar answered Oct 23 '22 18:10

dfsq