angularjs成功,然后成功返回正确响应,然后没有响应

时间:2015-12-20 14:55:38

标签: angularjs

我不知道当时和成功之间的区别我发布我的控制器和查看部分如果我使用成功它适用于我但我用它然后它不产生输出说不是在控制台中找到的数据然后也提供正确的数组不知道为什么它没有以正确的方式反映到视图部分可以任何人请建议并告诉差异以及提前感谢

    crudApp.controller("UsersController",function ($scope,$http,$timeout,localStorageService,cfpLoadingBar,$location,$route) {

        cfpLoadingBar.start();

        var token = localStorageService.get('token');

        //console.log(localStorageService);

        $http({
        url:base_url + 'api/loadUsers',
        method:"GET",
        withCredentials:true,
        headers:{'token':token}
        }).success(function(data, status, headers, config) {
            //console.log(data);

// It provides the correct result but i use then it does not return correct ans says No data found

// i used this code

//.then(function(data){

            $scope.list = data;
            $scope.currentPage = 1; //current page
            $scope.entryLimit = 10; //max no of items to display in a page
            $scope.filteredItems = $scope.list.length; //Initially for no filter
            $scope.totalItems = $scope.list.length;
            //here data is response from server. You can check status, headers, configuration settings too.
            cfpLoadingBar.complete();
        }).error(function(data, status, headers, config) {
            //do something here. Error occurd while fetching data from server
            cfpLoadingBar.complete();
        });

        $scope.setPage = function(pageNo) {
            $scope.currentPage = pageNo;
        };

        //delete user
        $scope.delete_user = function(did) {
            var token = localStorageService.get('token');

            if(token != null) {
                swal({   title: "Are you sure want to delete this record?",   text: "",   type: "warning",   showCancelButton: true,   confirmButtonColor: "#DD6B55",   confirmButtonText: "Yes",   closeOnConfirm: true },
                function(isConfirm){
                       if (isConfirm)   {
                                    var url = base_url + 'api/deleteUser/'+did;
                                    $http({
                                    url:url,
                                    method:"DELETE",
                                    withCredentials:true,
                                    headers:{'token':token}
                                    }).success(function(data, status, headers, config) {
                                            $location.path('/users');
                                            $route.reload();
                                    }).error(function(data, status, headers, config) {
                                        //do something here. Error occurd while fetching data from server
                                    });
                            }
                });
            } else $route.reload();
        };
    });


    <tr ng-repeat='data in filtered = (list | filter:search | orderBy : predicate :reverse) | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit' data-ng-class-odd="'odd'" data-ng-class-even="'even'">
                                        <td>{{ data.id }}</td>
                                        <td>{{ data.name }}</td>
                                        <td>{{ data.email}} </td>
                                        <td>{{ data.mobile }} </td>
                                        <td class="action"><a href="#/newuser/{{ data.id }}">Edit</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript://" data-ng-click="delete_user(data.id);">Delete</a>
                                </tr>
                                <tr ng-show="filteredItems <= 0"><td align="center" colspan="5" style="color:red;font-weight:bold;">No records found.</td></tr>


                            </table><br /><br />
                            <div ng-show="filteredItems > 0">
                                <div pagination="" max-size="10" ng-model="currentPage" boundary-links="true" total-items="filteredItems" items-per-page="entryLimit" class="pagination-small" previous-text="&laquo;" next-text="&raquo;" first-text="&lt;" last-text="&gt;" rotate="false">

                                </div>
                            </div>

1 个答案:

答案 0 :(得分:0)

忘记success()(和error())。它已被弃用,因此不应该再使用了。始终使用then()

除了链接之外,success()then()之间的主要区别在于success()有4个参数:datastatusheadersconfig;而then()只接受一个参数:HTTP响应对象,它有4个字段:datastatusheadersconfig

所以,当你有

.success(data) {
    // do something with data

将其替换为

.then(response) {
    // do something with response.data

当你有

.success(data, status, headers, config) {
    // do something with data, status, headers, config

将其替换为

.then(response) {
    // do something with response.data, response.status, response.headers, response.config