在模板中构建AngularJS HTTP请求

时间:2014-09-16 22:49:27

标签: ajax json angularjs http controller

我是AngularJS的新手,并从我的老板那里得到了一份WebApp模板。 我必须构建一个CRM,模板完全是AngularJS。 这是一个非常好的框架,但我遇到了问题。

我有这个控制器来处理这些文章:

(function () {
"use strict";
angular.module("app.tables", []).controller("articlesCtrl", ["$scope", "$http", "$filter",

    function ($scope, $http, $filter) {
        var init;
        $http.get('http://srv.ct-server.de/ctcrm/www/articles.php?method=fetchAll').success(function (data) {
            $scope.articles = data;
            console.log($scope.articles);
        }).
            error(function (data) {
                // log error
            })
            , $scope.searchKeywords = "", $scope.filteredArticles = [], $scope.row = "", $scope.select = function (page) {
            var end, start;
            return start = (page - 1) * $scope.numPerPage, end = start + $scope.numPerPage, $scope.currentPageArticles = $scope.filteredArticles.slice(start, end)
        }, $scope.onFilterChange = function () {
            return $scope.select(1), $scope.currentPage = 1, $scope.row = ""
        }, $scope.onNumPerPageChange = function () {
            return $scope.select(1), $scope.currentPage = 1
        }, $scope.onOrderChange = function () {
            return $scope.select(1), $scope.currentPage = 1
        }, $scope.search = function () {
            return $scope.filteredArticles = $filter("filter")($scope.articles, $scope.searchKeywords), $scope.onFilterChange()
        }, $scope.order = function (rowName) {
            return $scope.row !== rowName ? ($scope.row = rowName, $scope.filteredArticles = $filter("orderBy")($scope.articles, rowName), $scope.onOrderChange()) : void 0
        }, $scope.numPerPageOpt = [3, 5, 10, 20], $scope.numPerPage = $scope.numPerPageOpt[2], $scope.currentPage = 1, $scope.currentPageArticles = [], (init = function () {
            return $scope.search(), $scope.select($scope.currentPage)
        })()

    }
])}.call(this));

没有过滤器,它可以工作...... 但是当我放入最后一个

, $scope.numPerPageOpt = [3, 5, 10, 20], $scope.numPerPage = $scope.numPerPageOpt[2], $scope.currentPage = 1, $scope.currentPageArticles = [], (init = function () {
            return $scope.search(), $scope.select($scope.currentPage)
        })()

出现错误

  

TypeError:无法读取未定义的属性“slice”

数组仍在控制台中。 我不明白这个错误。

这是我的HTML:

    <section class="panel panel-default table-dynamic">
    <div class="panel-heading"><strong>Artikel</strong></div>

    <div class="table-filters">
        <div class="row">
            <div class="col-sm-4 col-xs-6">
                <form>
                    <input type="text"
                           placeholder="Search..."
                           class="form-control"
                           data-ng-model="searchKeywords"
                           data-ng-keyup="search()">
                </form>
            </div>
        </div>
    </div>

    <table class="table table-bordered table-striped table-responsive">
        <thead>
            <tr>
                <th><div class="th">
                    ID
                    <span class="fa fa-angle-up"
                          data-ng-click=" order('idArticles') "
                          data-ng-class="{active: row == 'idArticles'}"></span>
                    <span class="fa fa-angle-down"
                          data-ng-click=" order('-idArticles') "
                          data-ng-class="{active: row == '-idArticles'}"></span>
                </div></th>
                <th><div class="th">
                    Artikelgruppe
                    <span class="fa fa-angle-up"
                          data-ng-click=" order('artGroupName') "
                          data-ng-class="{active: row == 'artGroupName'}"></span>
                    <span class="fa fa-angle-down"
                          data-ng-click=" order('-artGroupName') "
                          data-ng-class="{active: row == '-artGroupName'}"></span>
                </div></th>
                <th><div class="th">
                    Artikelname
                    <span class="fa fa-angle-up"
                          data-ng-click=" order('artName') "
                          data-ng-class="{active: row == 'artName'}"></span>
                    <span class="fa fa-angle-down"
                          data-ng-click=" order('-artName') "
                          data-ng-class="{active: row == '-artName'}"></span>
                </div></th>
                <th><div class="th">
                    Preis
                    <span class="fa fa-angle-up"
                          data-ng-click=" order('artPrice') "
                          data-ng-class="{active: row == 'artPrice'}"></span>
                    <span class="fa fa-angle-down"
                          data-ng-click=" order('-artPrice') "
                          data-ng-class="{active: row == '-artPrice'}"></span>
                </div></th>
                <th><div class="th">
                    EGIS-Artikelnummer
                    <span class="fa fa-angle-up"
                          data-ng-click=" order('egisArtId') "
                          data-ng-class="{active: row == 'egisArtId'}"></span>
                    <span class="fa fa-angle-down"
                          data-ng-click=" order('-egisArtId') "
                          data-ng-class="{active: row == '-egisArtId'}"></span>
                </div></th>
                <th><div class="th">
                    Sichtbarkeit
                </div></th>
            </tr>
        </thead>
        <tbody>
            <tr data-ng-repeat="article in currentPageArticles">
                <td>{{article.idArticles}}</td>
                <td>{{article.artGroupName}}</td>
                <td>{{article.artName}}</td>
                <td>{{article.artPrice}}€</td>
                <td>{{article.egisArtId}}</td>
                <td class="articles_active text-center">{{article.active}}</td>
            </tr>
        </tbody>
    </table>

    <footer class="table-footer">
        <div class="row">
            <div class="col-md-6 page-num-info">
                <span>
                    Zeige
                    <select data-ng-model="numPerPage"
                            data-ng-options="num for num in numPerPageOpt"
                            data-ng-change="onNumPerPageChange()">
                    </select>
                    Einträge pro Seite
                </span>
            </div>
            <div class="col-md-6 text-right pagination-container">
                <pagination class="pagination-sm"
                            ng-model="currentPage"
                            total-items="filteredArticles.length"
                            max-size="4"
                            ng-change="select(currentPage)"
                            items-per-page="numPerPage"
                            rotate="false"
                            previous-text="&lsaquo;" next-text="&rsaquo;"
                            boundary-links="true"></pagination>
            </div>
        </div>
    </footer>
</section>

请帮帮我

2 个答案:

答案 0 :(得分:0)

您需要使用Angular提供的$http服务,并将$scope变量设置为以下内容:

$http({method: 'GET', url: '/db/articles.php' }).
    success(function(data, status, headers, config) {
        $scope.articles = data;
    }).
    error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });

请务必将$http服务注入您的控制器,如下所示:

angular.module("app.tables", []).controller("articlesCtrl", ["$scope", "$filter", "$http",
    function($scope, $filter, $http) {

答案 1 :(得分:0)

在您的模板文件中,您可以使用{{ articles }}ng-repeat来循环播放所有文章。控制器通过ng-controller="MyController"连接到模板中的部件。

这一切都非常基本,角度是多么有效。我建议你看一些教程...例如,这个:http://www.ng-newsletter.com/posts/beginner2expert-scopes.html