【问题标题】:not getting $rootScope.updatedata from controller to html view没有从控制器获取 $rootScope.updatedata 到 html 视图
【发布时间】:2018-05-16 15:03:36
【问题描述】:

我正在尝试使用 angularjs 的 $rootScope 从控制器获取 $rootScope.updatedata 到视图。当我将从服务器响应获得的数据传递给视图时,数据为空。来自控制器的数据是有价值的。这是sn-ps

app.controller("updateCtrl",["$scope","$rootScope","$location","$cookies","$http",
                            function($scope,$rootScope,$location,$cookies,$http){
    getUserDetails: function (email) {
            $http({
                method: 'GET',
                url: '/updateprofile',
                params: {email: email}
            }).then(function successCallback(response) {
                if (response.status == 204) {
                } else if (response.status == 200) {


                    alert("email24 "+response.data); //this returns an object
                    $rootScope.updatedata = response.data; //passing to the view                }
            }, function errorCallback(response) {
                alert(JSON.stringify(response));
            });
        }
    }

这是我在视图中的获取方式

<input formcontrolname="firstName" value="{{updatedata.email}}" />

弹簧控制器sn-ps

@RequestMapping(value = {"/updateprofile"}, method = RequestMethod.GET, headers = "Accept=application/json") 
    public ResponseData<Client> updateByEmail( @RequestParam("email") String email) {

这是我的挑战

【问题讨论】:

  • 您是否有一个控制器(我之所以问是因为您如何在该输入中访问updatedata.email)以及您在哪里调用getUserDetails 方法?请在调用它的地方添加一些代码。
  • 是的,我有控制器......点击它时我正在调用 getUserDetails>>> 一个 ng-click="getUserDetails.getUserDetails(user.email)" class="ui" href= "#/dashboard">编辑 个人资料
  • 能否请您在定义此控制器的位置添加代码?
  • 这里是 >>>> app.controller("updateCtrl",["$scope","$rootScope","$location","$cookies","$http", function ($scope,$rootScope,$location,$cookies,$http){
  • 控制器应该避免使用$rootScope。见AngularJS FAQ - $rootScope exists, but it can be used for evil

标签: angularjs json spring


【解决方案1】:

在这种情况下使用$rootscope 对您没有帮助。一般来说,尽量避免使用它,这是一种不好的做法。试试这样的:

app.controller("updateCtrl",["$scope","$location","$cookies","$http",
                            function($scope,$location,$cookies,$http){
    $scope.updatedata = {
      email: "",
      //rest of your fields
    };
    getUserDetails: function (email) {
            $http({
                method: 'GET',
                url: '/updateprofile',
                params: {email: email}
            }).then(function successCallback(response) {
                if (response.status == 204) {
                } else if (response.status == 200) {


                    alert("email24 "+response.data); //this returns an object
                    $scope.updatedata.email = response.data.email; 
                    //populate rest of the fields
            }
            }, function errorCallback(response) {
                alert(JSON.stringify(response));
            });
        }
    }

【讨论】:

  • 返回 undefined >>> alert("email44 "+response.data.email);所以解决方案不起作用
  • console.log(response.data) 返回什么?
  • 它返回实际的预期数据
  • 地址:“wrerere”城市:“俄亥俄州”confirmedEmail:真实国家:“6”dateOfReg:空出生日期:“2018-05-17”电子邮件:“meme@yahoo.com”名字:“ sdsdss" 性别:"2: MALE" id:37 姓氏:"sdsds" 中间名:"dsdsds" nationalidentificationnumber:"kjkjkjkjk" 国籍:"4" 密码:"password" 电话号码:"09009009876" 状态:"39" 用户名:"银”
  • 但是选择一个值是个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-04
  • 2010-09-22
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多