2017-07-31 139 views
0

我創建了一個服務,一旦他登錄,所以我可以養活這對我所有的角控制器獲得用戶的詳細信息:AngularJS服務:無法訪問子屬性

var app = angular.module('myApp', ['ui.calendar', 'ui.bootstrap']); 

app.service('authUser', ['$http', function ($http) { 
var _currentUser = {}; 
$http.get("/Home/GetUser", { 
    cache: false 
}).success(function (data) { 
    _currentUser.userid = data.userId; 
    _currentUser.role = data.userRole; 
    _currentUser.departmentid = data.departmentId; 
    _currentUser.department = data.userDepartment; 
}); 
this.currentUser = _currentUser; 
}]); 

app.controller('eventController', ['$scope', 'authUser', '$http', 'uiCalendarConfig', '$uibModal', function ($scope, authUser, $http, uiCalendarConfig, $uibModal) { 
$scope.currentUser = authUser.currentUser; 

當我嘗試顯示輸出,它看起來好嗎:

<div> 
    <label>{{currentUser}}</label> 
    <label>Id: {{currentUser.userid}}, Role: {{currentUser.role}}, Department Id: {{currentUser.departmentid}}, Department Name: {{currentUser.department}}</label> 
</div> 

此輸出:

{"userid":"29aa607a-d36b-46bb-a3a7-16bead5f4706","role":"Super 
Admin","departmentid":1,"department":"MIS"} Id: 29aa607a-d36b-46bb-a3a7-16bead5f4706, Role: Super Admin, Department Id: 1, Department Name: MIS 

但是當我嘗試使用銅的性質在我的代碼中,rrentUser沒有任何內容被傳遞。例如:

$http.get("/Home/GetVenues", { 
    cache: false, 
    params: { 
     currentdepartment: $scope.currentUser.departmentid 
    } 
}).success(function (data) { 
    // ... 
}) 

我試圖

$scope.currentdept = $scope.currentUser.departmentid; 

或者

$scope.currentdept = authUser.currentUser.departmentid; 

但是這兩種回報什麼;米;

我很抱歉,如果我在這裏錯過了一些非常簡單的東西,但我是一個馬鈴薯請憐憫; m;

如果在這裏所有相關的是從我的HomeController我的getUser方法:

public JsonResult GetUser() 
    { 
     if (Request.IsAuthenticated) 
     { 
      ApplicationDbContext db = new ApplicationDbContext(); 
      var user = db.Users.Find(User.Identity.GetUserId()); 
      var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(ApplicationDbContext.Create())); 
      string id = userManager.FindById(User.Identity.GetUserId()).Id; 
      IList<string> roleNames = userManager.GetRoles(id); 
      ReservationsDatabaseModel dept = new ReservationsDatabaseModel(); 
      Department department = dept.Departments.Find(user.DepartmentID); 

      userDepartmentRole user_department_role = new userDepartmentRole(); 
      user_department_role.userId = user.Id; 
      user_department_role.departmentId = user.DepartmentID; 
      user_department_role.userDepartment = department.Name; 
      user_department_role.userRole = roleNames.FirstOrDefault(); 

      return new JsonResult { Data = user_department_role, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; 
     } 
     else 
     { 

      userDepartmentRole user_department_role = new userDepartmentRole(); 
      user_department_role.userId = "0"; 
      user_department_role.departmentId = 0; 
      user_department_role.userDepartment = "0"; 
      user_department_role.userRole = "Guest"; 
      return new JsonResult { Data = user_department_role, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; 
     } 
    } 
+0

在成功函數裏面設置'this.currentUser = _currentUser;' – Vanojx1

+0

我試過了,但現在輸出的是 $ scope.currentUser = authUser.currentUser;變成空的:Id:,作用:,部門Id:,部門名稱:; m; – Akii

+0

你可以給plunker鏈接嗎? –

回答

0

取回/Home/GetVenues需要等待authUser數據從服務器到達。

更改服務,以節省一個承諾:

app.service('authUser', ['$http', function ($http) { 
    var _ = this; 
    this.currentUser = {}; 

    this.currentUserPromise = $http.get("/Home/GetUser", { 
     cache: false 
    }).then(function (response) { 
     var data = response.data; 
     _.currentUser.userid = data.userId; 
     _.currentUser.role = data.userRole; 
     _.currentUser.departmentid = data.departmentId; 
     _.currentUser.department = data.userDepartment; 
     return data; 
    }); 
}]); 

然後使用控制器的諾言:

authUser.currentUserPromise.then(function(userData) { 

    $http.get("/Home/GetVenues", { 
     cache: false, 
     params: { 
      currentdepartment: userData.departmentid 
     } 
    }).then(function (response) { 
     var data = response.data; 
     // ... 
    }) 

}); 

這種方式爲authData控制器等待從服務器從獲取之前到達/Home/GetVenues

+0

對於遲到的回覆感到抱歉,我們停電了,所以我只是睡覺了; m;謝謝你解釋問題是什麼:D我跟着這個,現在它正在工作,非常感謝^〜^ – Akii

0

您的電話應只工作返回GetUser API後,您可以通過將GetVenues上的呼叫按鈕點擊,而不是測試在init上運行。