AngularJS $ http get不起作用,但$ .ajax会起作用

时间:2014-10-29 13:36:08

标签: javascript jquery ajax angularjs get

我是AngularJS的新手。我似乎无法获得$ http工作。我有以下工厂:

app.factory('employeeFactory', function ($http) {
    var factory = {};

    // get data form controller
    var employees = [];
    var Url = "../../../Employee/GetEmployees";

    // this does not work ----------------------------
    $http.get(Url, { params: { term: 'Step' }}).
      success(function (response, status, headers, config) {
          employees = response.data
      }).
      error(function (response, status, headers, config) {
          alert(error);
      });

    // this works using JQuery ajax ----------------------------
    $.ajax({
        url: Url,
        data: { term: 'Step' },
        dataType: "json",
        type: "GET",
        error: function (request, status, error) {
            alert(error);
        },
        success: function (response) {
            $.each(response.data, function (i, obj) {
                employees.push({ EmployeeName: obj.EmployeeName, EmployeeNumber: obj.EmployeeNumber });
            });
        }
    });

    factory.getEmployees = function () {
        return employees
    };

    return factory;
});

以下控制器:

app.controller('EmployeeController', function ($scope, employeeFactory) {
    $scope.employees = [];
    init();
    function init() {
        $scope.employees = employeeFactory.getEmployees();
    }
});

工厂中的ajax调用有效但$ https没有(两者都在工厂,我只是在测试时注释掉一个或另一个)。我查看了谷歌chrome dev工具,并且两个调用都以相同的格式返回数据,但是$ http数据没有绑定到html:

<div class="container">
    <h4>This is view 1</h4>
    Type a name to filter: <input type="text" data-ng-model="employeeSearch" />
    <ul>
        <li data-ng-repeat="employee in employees | filter:employeeSearch | orderBy:'EmployeeName'">{{ employee.EmployeeName  }} - {{ employee.EmployeeNumber }}</li>
    </ul>
</div>

以下是两次调用的工厂返回格式:

{data: [{EmployeeNumber:123456, EmployeeName:Johnson,Bob},…]
data: [{EmployeeNumber:123456, EmployeeName:Johnson,Bob},…]
    0: {EmployeeNumber:123456, EmployeeName:Johnson,Bob}
        EmployeeName: "Johnson,Bob"
        EmployeeNumber: "123456"

我不明白为什么当两个调用都以相同的格式将数据返回到视图时,$ http方法不会发生绑定。任何帮助表示赞赏

2 个答案:

答案 0 :(得分:4)

jQuery ajax的工作原理是因为你推送到返回的引用 在angular ajax success handler中,你覆盖变量,但返回值仍然是空引用。

因此,要使angular $ http函数正常工作,您应该在成功处理程序中执行以下操作:

angular.forEach(response.data, function(value) {
    employees.push(value);
});

答案 1 :(得分:0)

使用此代码:

服务:

app.factory('employeeFactory', function ($http) {
    var employees = [];
    var Url = "../../../Employee/GetEmployees";
    var factory = {
     getEmp:function(){
         return $http.get(Url, { params: { term: 'Step' }})
     }
    }
    return factory;
});

控制器:

app.controller('EmployeeController', function ($scope, employeeFactory) {
    $scope.employees = [];

    function init() {
        employeeFactory.getEmp().then(function(data){
         $scope.employees=data;
        })
        .catch(function(err){
         console.log(err);
        })
    }
  init();
});