【发布时间】: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 -$rootScopeexists, but it can be used for evil