2016-12-05 112 views
0

我想從控制器訪問屬性「狀態」,只需執行一些操作,但我無法獲取此屬性並執行任何其他操作。我分享我下面的代碼:無法從響應中獲取單個屬性Angular 2

TasksController:

[HttpGet] 
public ActionResult GetTasks() 
{ 
    var q = (from a in db.Tsk 
     join b in db.TType on a.TaskTypeID equals b.TaskTypeID 
     join c in db.Prior on a.PriorityID equals c.PriorityID 
     join d in db.Usr on a.AssignedTo equals d.Employees.EmpName 
     select new 
     { 
      a.TaskID, 
      a.TaskCode, 
      a.AssignedTo, 
      a.Date, 
      a.DueDate, 
      a.Status, 
      a.Reply, 
      a.PriorityID, 
      a.TaskTypeID, 
      b.TaskType, 
      c.Priorities, 
      d.Login 
     }).ToList().Skip(1).AsEnumerable(); 
     db.Configuration.ProxyCreationEnabled = false; 
     return Json(q, JsonRequestBehavior.AllowGet); 
} 

AppService服務:

import { Injectable } from '@angular/core'; 
import { Http, Headers, Response } from '@angular/http'; 
import 'rxjs/add/operator/map'; 
import 'rxjs/add/operator/filter'; 
import { Observable } from 'rxjs/Observable' 

@Injectable() 

export class AppService { 
constructor(private _http: Http) { } 

//Task Register 
getFTRs(c: string) { 
return this._http.get('Tasks/GetTasks').map(res => res.json().filter(a => a.Login === c)); 
} 
} 

HomeComponent:

import { Component, OnInit, Input } from '@angular/core'; 
import { AuthenticationService } from '../_services/index'; 
import { AppService } from '../app.service'; 
import { LoginComponent } from '../login/index'; 
import { User, TaskRegisters } from '../contract'; 
import { Message } from '../message'; 

@Component({ 
    moduleId: module.id, 
    selector: 'home', 
    templateUrl: 'home.component.html', 
    providers: [LoginComponent] 
}) 

export class HomeComponent implements OnInit { 
    users: User[]; 
    tasks: string; 
    msgs: Message[] = []; 
    curr: any; 
    constructor(private userService: AuthenticationService, 
     private Tsk: AppService, 
     private Log: LoginComponent) { } 

    ngOnInit() { 
     debugger; 
     this.curr = localStorage.getItem('currentUser').slice(1); 
     this.Tsk.getFTRs(this.curr).subscribe(
      res => { 
       this.tasks = res.Status, 
        error => console.log(error) 
      }); 
     if (this.tasks) { 
      this.msgs.push({ severity: 'error', summary: 'Task Assigned', detail: 'A new task has been assigned to you' }) 
     } 
    } 
} 

狀態是一個布爾值,如果布爾是true,我想在消息數組中推送消息。我無法獲得Status的值並將其存儲在Home組件的任務變量中。每當我運行該程序時,都會顯示此任務爲未定義的,因此無法進行比較。任何幫助將不勝感激。

回答

1

變化:

this.Tsk.getFTRs(this.curr).subscribe(
      res => { 
       this.tasks = res.Status, 
        error => console.log(error) 
      }); 
     if (this.tasks) { 
      this.msgs.push({ severity: 'error', summary: 'Task Assigned', detail: 'A new task has been assigned to you' }) 
     } 

this.Tsk.getFTRs(this.curr).subscribe(
      (res) => { 
       console.log(res); //What does this print? 
       this.tasks = res.Status; 
       console.log(this.tasks); //What does this print? 
       if (this.tasks) { 
         this.msgs.push({ severity: 'error', summary: 'Task Assigned', detail: 'A new task has been assigned to you' }) 
       } 
      }, 
      (error) => {console.log(error);} 
    ); 

既然你assingning this.tasksgetFRSs'回調是異步,你的時間在下面用它在if聲明它是undefined

+0

this.tasks仍然是不確定的 –

+0

@RehanSiddiqui什麼呢'的console.log(RES);'打印內部認購?檢查我更新的答案。 – echonax

+0

它記錄一個[對象含有AssignedTo: 「賈漢吉爾」 日期: 「/日期(1480705200000)/」 DUEDATE: 「/日期(1481137200000)/」 登錄: 「賈漢吉爾」 優先級: 「緊急」 PriorityID: 2 回覆:假 狀態:真正 TaskCode: 「03/2」 的TaskID:4 任務類型: 「個人」 TaskTypeID:1 –

0

由於this.tasks現在提供給我@echonax的編輯我這樣做是爲了使工作後!

ngOnInit() { 
     debugger; 
     this.curr = localStorage.getItem('currentUser').slice(1); 
     this.Tsk.getFTRs(this.curr).subscribe(
      res => { 
       this.tasks = res; 
       for (let i = 0; i < this.tasks.length; i++){ 
        if (this.tasks[i].Status) { 
         this.msgs.push({ severity: 'error', summary: 'Task Assigned', detail: 'A new task has been assigned to you' }) 
        } 
       } 
      }, 
      error => console.log(error) 
     )} 
相關問題