angularJS 1.2代码无法正常工作

时间:2017-12-12 06:53:22

标签: angularjs

这是我的AngularJS 1.2代码,它没有按预期工作。

你能看看并帮助我吗?

angular.module('myApp', [])
  .controller('customersCtrl', ['$scope', '$http', function ($scope, $http) {
  $scope.getPeople = function (count) {
    // json callback not working
    $http.jsonp('http://www.filltext.com/?rows=' + count +
      '&fname = { firstName } & callback = JSON_CALLBACK ').success(function (data) {
      $scope.people = data;
    });
  };
  $scope.countSelection = 10;
  $scope.getPeople($scope.countSelection);
}]);

2 个答案:

答案 0 :(得分:0)

它会转到$ http错误回调,因为你在'&amp ;;中提供了空格callback = JSON_CALLBACK'必须没有空格,因此代码将为:

$http.jsonp('http://www.filltext.com/?rows=' + count +
                '&fname={firstName}&callback=JSON_CALLBACK')

Here is Working Demo for your code

答案 1 :(得分:0)

试试这个

var app = angular.module('myApp', []);
app.controller('customersCtrl', ['$scope', '$http', function ($scope, $http) {
  $scope.getPeople = function (count) {
    // json callback not working
    $http.jsonp('http://www.filltext.com/?rows=' + count +
      '&fname = { firstName }', function (err, data) {
      if (err) {
        console.log(err);
      } else {
        console.log(data);
      }
    });
  };
  $scope.countSelection = 10;
  $scope.getPeople($scope.countSelection);
}]);