在以角度进行布线后,数据未从控制器填充到视图中

时间:2016-12-01 14:38:33

标签: javascript angularjs

我在视图中看不到任何数据(Html)。我在哪里从服务中获取数据到控制器。
请让我知道我错过了什么。

任何我缺少的建议

控制器:

(function () {
    'use strict';

    angular.module('app') .controller('UserController', UserController);
    UserController.$inject = ['$scope', 'UserService', '$http', '$filter', '$rootScope','$location','$window'];
    function UserController($scope, UserService, $http, $filter, $rootScope,$location,$window) {


        $scope.editUser = function(userDetails){
            UserService.editUser(userDetails).then(editUserSuccess,editUserFailure);
            $location.path('edit');
        }


        var editUserFailure =function (error){
        };

        var editUserSuccess = function (response) {
             var userRow = response.data;
                $scope.edituserData=userRow;            
        };
    };  
})();

这是我的服务部分

(function () {
    'use strict';

    angular.module('app').factory('UserService', UserService);

    UserService.$inject = ['$http', '$rootScope'];
    function UserService($http, $rootScope) {
        var service = {};
        service.editUser = editUser;
        return service;
        function editUser(editUserObj){
            console.log(editUserObj.userId);
            return $http({
              method: 'POST',
              url: 'editUser',
              data: btoa(editUserObj.userId),
              contentType: "application/json; charset=utf-8",
            });
        }
    }
})();

我从应用程序路由

appname.config(function($routeProvider) {
    $routeProvider
      .when("/edit", {
        templateUrl:"my.html",
        controller : 'UserController'
    });

});


my.html:
--------

When i am trying to get data in my html template 

 <div class="row">
                    <div class="col-lg-6 col-sm-6 col-xs-6">
                      <p class="text-right"><strong>Middle Name :</strong></p>
                    </div>
                    <div class="col-lg-6 col-sm-6 col-xs-6">
                      <p class="text-left-user"> {{editUserData.screenName}}</p>
                    </div>
                  </div>

提前致谢

1 个答案:

答案 0 :(得分:1)

您的服务只需要在成功时返回数据。现在它只返回Promise。

(function () {
    'use strict';

    angular.module('app').factory('UserService', UserService);

    UserService.$inject = ['$http', '$rootScope'];
    function UserService($http, $rootScope) {
        var service = {};
        service.editUser = editUser;
        return service;
        function editUser(editUserObj){
            console.log(editUserObj.userId);
            return $http({
              method: 'POST',
              url: 'editUser',
              data: btoa(editUserObj.userId),
              contentType: "application/json; charset=utf-8",
            }).success(function(response){
                return response;
            });
        }
    }
})();

然后在控制器的.then成功功能中,您应该有响应。