Pass json string as parameter in ng-click

I want to pass a JSON string to 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: // The code goes here

 var app = angular.module('app', []); app.controller('appCtrl', ['$scope', function($scope) { $scope.go = function(parm) { alert('hi'); }; } ]); 

Plunker

+5
source share
1 answer

There are two problems. First, you need to declare the ngController ng-controller="appCtrl" for some element. Secondly, you need to take the ngClick attributes in quotes, and then pass the object without the quotes to the go function. Angular will understand that you are passing and an 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

+4
source

Source: https://habr.com/ru/post/1213111/


All Articles