簡體   English   中英

無法從響應Angular 2獲取單個屬性

[英]Unable to get single property from response 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' })
        }
    }
}

Status是一個布爾值,如果boolean為true ,我想將消息推送到msgs數組中。 我無法獲取Status的值並將其存儲在home組件的task變量中。 每當我運行程序時,它將this.tasks顯示為未定義,因此無法進行比較。 任何幫助將不勝感激。

更改:

 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

由於在@echonax編輯之后,現在可以使用this.tasks了,所以我這樣做是為了使它起作用!

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)
        )}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM