无法从响应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组件的tasks变量中 . 每当我运行程序时,它都显示this.tasks为undefined,因此无法进行制作和比较 . 任何帮助将不胜感激 .

回答(2)

2 years ago

更改:

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

由于你正在 getFRSs' 回调 getFRSs' 回调 async ,当你在 if 语句中使用它时,它是 undefined .

2 years ago

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