我想将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');
};
}
]);
答案 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>