简体   繁体   English

如何在javascript中使用函数参数成功传递响应数据?

[英]How can I pass the response data successfully with function parameter in javascript?

I am working on single page application, which is implementing based on Angular JS, JavaScript, JSON. 我正在研究基于Angular JS,JavaScript,JSON的单页应用程序。 I am calling one API then I am getting response and this response I want pass as a parameter in function call. 我先调用一个API,然后得到响应,然后将此响应作为参数传递给函数调用。 I am sharing my code: 我正在分享我的代码:

abc.JS abc.js

 Rh.get('------API URL----').then(function(response){    
    var data=response; 
          var item = '<a class="btn btn-xs" ng-click="sendDataPost('+"'"+JSON.stringify(data)+"'"+')" >'   
           '</a>'
    })

 $scope.sendDataPost = function (data) {
    alert("--");     
    console.log(data);
}

When I click on anchor tag then sendDataPost function is call and comes into the function definition($scope.sendDataPost). 当我单击锚标记时,将调用sendDataPost函数,并进入函数定义($ scope.sendDataPost)。 only [object Object] is showing in console. 控制台中仅显示[object Object]。 I am not able to pass JSON Data as a parameter. 我无法将JSON数据作为参数传递。 I have seen one link but still I am not able to do because in this link using onclick, but I am using ng-click. 我已经看到一个链接,但仍然无法执行操作,因为在此链接中使用的是onclick,但我使用的是ng-click。 How to pass a json object in js funcion as a function parameter? 如何在js函数中将json对象作为函数参数传递?


I am sharing my sample URL. 我正在分享我的示例网址。
JSON SAMPLE URL . JSON SAMPLE URL

http://www.json-generator.com/api/json/get/cgwLpLgbyq?indent=2 http://www.json-generator.com/api/json/get/cgwLpLgbyq?indent=2

I editing my question. 我编辑我的问题。 I solve when I am parsing the data in parameter, It is breaking. 当我解析参数中的数据时,我解决了,它正在中断。 I am sharing screen shot 我正在分享屏幕截图
在此处输入图片说明

sendDataPost parameter is breaking in console and breaking html page. sendDataPost参数在控制台中中断并且在html页面中中断。 All json data display in HTML page. 所有json数据都显示在HTML页面中。 How can I pass the response data successfully with function parameter. 如何使用功能参数成功传递响应数据。

Why not you try to holding value in scope variable this below? 为什么不尝试在下面的范围变量中保留值?

$scope.result = [];
Rh.get('------API URL----').then(function(response){    
    $scope.result = response;
})

$scope.sendDataPost = function (data) {    
    console.log(data);
}

in HTML 在HTML中

ng-click="sendDataPost(result)" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在javascript中,如何传递带有参数的函数以供以后调用? - In javascript, how can I pass a function with parameter to be called later? 如何将 JavaScript ES6 箭头函数作为参数传递 - How can I Pass a JavaScript ES6 Arrow Function as a Parameter 如何将Smarty的参数传递给javascript函数? - How can I pass a parameter of Smarty to a javascript function? 如何将另一个参数传递给 javascript map() function - how can I pass another parameter to javascript map() function 我可以将“this”作为参数传递给javascript中的另一个函数吗 - Can I pass "this" as a parameter to another function in javascript 如何在函数中传递$ scope.gridOptions.data作为参数 - How can I pass $scope.gridOptions.data as parameter in function 如何在JavaScript中将函数作为参数传递 - How do I pass function as a parameter in javascript 如何将返回响应的预定义JavaScript回调传递给函数作为参数? - How to pass a predefined javascript callback that returns a response, into a function as a parameter? 我如何使用javascript中的ajax调用将参数或ajax数据传递给python脚本并在成功时获得响应? - how do i pass parameter or ajax data to a python script using ajax call in javascript and get response if it is successful? 我如何将函数作为参数传递给参数 - How can i pass function as parameter with arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM