无法使用AngularJS Factory

时间:2016-09-12 12:12:59

标签: javascript angularjs angularjs-factory

我尝试使用http请求在表格中显示一些信息。

为此,我创建了一个工厂,我在控制器内调用该工厂。当我验证(使用FireBug)'$ scope.showSal'是否有数据时,它告诉我它存在一个包含4个元素的数组(完全没问题)。

问题是屏幕上没有显示任何数据。

使用像$ scope.showSal = [{...}]这样的简单赋值,但是当我使用工厂时,它不起作用。

有人可以帮我使用工厂收到的数据吗?

PS:对于测试我使用特定的数组返回而不返回response.data(但即使我返回response.data也有相同的行为)

var app = angular.module("salariesApp", ['ui.bootstrap']);

app.factory('salServ', ['$http', function ($http) {
    var showSal = {};

    showSal.getItems = function () {
    return $http.get('http://localhost:8080/TT2/GetSalariesDetails')
            .then(
            function (response) {
                alert("OK");
                return [{ id: '1', an: '2016', den: 'Oracle Romania' },
                            { id: '2', an: '2016', den: 'Microsoft' },
                            { id: '3', an: '2016', den: 'Computer Generated Solutions - CGS Europe' },
                            { id: '4', an: '2016', den: 'IBM' }];
            },
            function (response) {
              alert("NO");
            }
          );
    };
    return showSal;
}]);

app.controller('mainCtrl', ['$scope', 'salServ', function($scope, salServ) {    
    $scope.showSal = salServ.getItems();
    console.log($scope.showSal);

    $scope.showItems = $scope.showSal;
}]);

HTML代码在这里:

<!DOCTYPE html>
<html>

  <head>
    <title>Research project</title>
    <link data-require="bootstrap-css@3.3.6" data-semver="3.3.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js" data-semver="3.0.0" data-require="jquery@*"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js" data-semver="1.5.7" data-require="angularjs@1.5.7"></script>
    <script data-require="ui-bootstrap-tpls-0.12.0.min.js@*" data-semver="0.12.1" src="https://raw.githubusercontent.com/angular-ui/bootstrap-bower/0.12.1/ui-bootstrap-tpls.min.js"></script>
    <script src="test.js"></script>
  </head>

  <body ng-controller="mainCtrl" ng-app="salariesApp">
    <table class="table">
      <thead>
        <tr>
          <th ng-click="sortBy(id)">Nr. crt.</th>
          <th ng-click="sortBy(an)">Anul</th>
          <th ng-click="sortBy('den')">Denumirea institutiei/companiei</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="tableSearch"></td>
          <td class="tableSearch">
            <input type="text" ng-value="{{crrYear}}" ng-model="searchSal.an" id="inputYear" class="form-control" />
          </td>
          <td class="tableSearch">
            <input type="text" ng-model="searchSal.den" id="inputName" class="form-control" />
          </td>
        </tr>
        <tr ng-repeat="rows in showItems">
          <td class="spa-col-md-1">{{rows.id}}</td>
          <td class="spa-col-md-2">{{rows.an}}</td>
          <td class="col-md-5">{{rows.den}}</td>
        </tr>
      </tbody>
    </table>
  </body>

</html>

2 个答案:

答案 0 :(得分:2)

$http.get() 异步并返回Promise。当您尝试设置$scope.showItems值时,HTTP调用肯定没有完成。

尝试

var app = angular.module("salariesApp", ['ui.bootstrap']);

app.factory('salServ', ['$http', function ($http) {
    var showSal = {};

    showSal.getItems = function (callBack) {
        return $http.get('http://localhost:8080/TT2/GetSalariesDetails')
                .then(
                callBack (response.data),
                function (response) {
                    alert("NO");
                }
              );
    };
    return showSal;
}]);

app.controller('mainCtrl', ['$scope', 'salServ', function ($scope, salServ) {
    salServ.getItems(
        function (data) {
            $scope.showItems = data;
            console.log($scope.showItems);//or $log...
        }
    );
}]);

答案 1 :(得分:1)

不要在angular中信任console.log函数,因为控制台浏览器可以在加载后获取信息。改为使用$ log:)

您可以根据范围值添加包含div的ng-if,如:

<table class="table" ng-if="showItems">

只有在showItems

中收到了项目时才会生成表格
相关问题