我創建了一個服務,一旦他登錄,所以我可以養活這對我所有的角控制器獲得用戶的詳細信息: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 };
}
}
在成功函數裏面設置'this.currentUser = _currentUser;' – Vanojx1
我試過了,但現在輸出的是 $ scope.currentUser = authUser.currentUser;變成空的:Id:,作用:,部門Id:,部門名稱:; m; – Akii
你可以給plunker鏈接嗎? –