除了从http请求接收的数据之外,还将参数传递给angularJS中的方法

时间:2021-07-27 19:43:24

I have this method in my controller that get a json file from my API.

我在我的控制器中有这个方法从我的API获取一个json文件。

      $scope.get = function (code) {

          api.get(code)
              .then(onJsonPart, onError);
      };

Doing this:

这样做:

      function onJsonPart(json) {

          console.log(json);
      }

I can print out the JSON I received, but I want to pass multiple variables to the onJsonPart method. Something like:

我可以打印出我收到的JSON,但我想将多个变量传递给onJsonPart方法。就像是:

      $scope.get = function (code) {

          api.get(code)
              .then(onJsonPart(code, data), onError);
      };

Here I get ERROR: data is not defined obviously, question is how do I define the data variable with the data received.

这里我得到错误:数据没有明确定义,问题是我如何用收到的数据定义数据变量。

and then have two parameters for my onJsonPart function like this:

然后我的onJsonPart函数有两个参数,如下所示:

      function onJsonPart(code, json) {

          console.log('Code: ' + code);
          console.log('json:');
          console.log(json);
      }

3 个解决方案

#1


2  

The code below will pass codeand the API's answer res to onJsonPart():

下面的代码将传递代码,API的答案将传递给onJsonPart():

  $scope.get = function (code) {

      api.get(code)
          .then(
              function(res) {
                  //...some place for logic
                  onJsonPart(code, res);
              },
              function (err) {
                  //...some place for logic
                  onError(err));
              }
      );
  };

#2


0  

Try

尝试

api.get(code)
.then(function (json) {
    onJsonPart(code, data);
}, onError);

#3


0  

You could do this:

你可以这样做:

$scope.get = function (code) {
      api.get(code)
          .then(function(data) {
             onJsonPart(code, data)...
          }
  };

#1


2  

The code below will pass codeand the API's answer res to onJsonPart():

下面的代码将传递代码,API的答案将传递给onJsonPart():

  $scope.get = function (code) {

      api.get(code)
          .then(
              function(res) {
                  //...some place for logic
                  onJsonPart(code, res);
              },
              function (err) {
                  //...some place for logic
                  onError(err));
              }
      );
  };

#2


0  

Try

尝试

api.get(code)
.then(function (json) {
    onJsonPart(code, data);
}, onError);

#3


0  

You could do this:

你可以这样做:

$scope.get = function (code) {
      api.get(code)
          .then(function(data) {
             onJsonPart(code, data)...
          }
  };