简体   繁体   English

我的HTML模板未在Angular2中收到我所有的组件变量

[英]My HTML Template is not receiving all of my component variables in Angular2

So my issue is I have 3 variables I would like to send to my html template but it only ever picks up one of them. 所以我的问题是我有3个变量要发送到我的html模板,但它只能选择其中一个。

My html template looks like this. 我的html模板看起来像这样。

<div class="page-data">
    <form method="post" action="api/roles/edit/{{role?.ID}}" name="{{role?.ID}}">

        <table cellpadding="11">
            <tr>
                <div class="label"> Role Name </div>
                    <input type="text" name="role_name" value={{role?.ROLE_NAME}}>
                <br>
                <br>
                <div class="label"> Description</div>
                    <input type="text" name="description" value={{role?.DESCRIPTION}}>

                <br>
                <br>
                <div class="label" *ngIf="role?.ACTIVE_FLAG === 'Y'"> Active Record </div>
                    <input type="radio" name="active_flag" value="Y" checked> Active
                    <input type="radio" name="active_flag" value="N"> Inactive
                <div *ngIf="role?.ACTIVE_FLAG === 'N'">
                    <input type="radio" name="active_flag" value = "Y"> Active
                    <input type="radio" name="active_flag" value= "N" checked> Inactive
                </div>
                <br>
                <br>
            </tr>
        </table>

        <div class="label"> Active Modules</div>
        <select id="modules_applied" name="module_name">
            <option value="none"> None</option>
            <option *ngFor="#module of modules" value = "{{module.MODULE_NAME}}">{{module.MODULE_NAME}}</option>
        </select>

        <div class="data-table">

            <table id="modules_table" border="1" cellpadding="7" cellspacing="7"></table>

            <br>
            <br>

        </div>
        <div class="label"> Inactive Modules </div>
        <select id="new_mods_select" name="new_modules">
            <option value="none"> None</option>
            <option *ngFor="#module of new_modules" value = "{{module.MODULE_NAME}}">{{module.MODULE_NAME}}</option>
        </select>
        <div class="data-table">

            <table id="new_modules_table" border="1" cellpadding="7" cellspacing="7"></table>

            <br>
            <br>
        </div>
        <input type="submit" name="submit" value="Save">
    </form>
</div>

and my component 我的组件

import {Component} from 'angular2/core';
import { CORE_DIRECTIVES } from 'angular2/common';
import {RoleService} from './../services/roles.services';
import {OnInit} from 'angular2/core';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import {RouteParams, RouterLink} from 'angular2/router';


@Component({
  selector: 'edit_role',
  providers: [RoleService],
  directives: [CORE_DIRECTIVES],
  templateUrl: 'app/roles/edit_role.html'
})
export class RoleEdit implements OnInit{

    role: any;
    modules: any;
    new_modules: any;
    params: any;

    constructor(private _roleService: RoleService, params:RouteParams){
        this.params = params.get('id');
    };

    ngOnInit(){
        this.getEditRoles(this.params);
    };

    getEditRoles(id){
        this._roleService.getEditRoles(id).subscribe(role_edit =>
            {this.role = role_edit.data[0],
            this.modules = role_edit.modules[0],
            this.new_modules = role_edit.new_modules[0]},
            error => {
                console.log('error logged:');
                console.log(error);
            }
        );
    };
}

My error 我的错误

Cannot find a differ supporting object '[object Object]' in [modules in RoleEdit@29:11] 在[RoleEdit @ 29:11中的模块]中找不到其他支持对象“ [对象对象]”

I just learned about the question marks for async calls with: {{role?.ID}}. 我刚刚了解了使用{{role?.ID}}进行异步调用的问号。 It does look like ngFor loops don't like those though, and I think that is most likely where my problem is. 看起来ngFor循环确实不喜欢这些循环,我认为这很可能是我的问题所在。 Thanks! 谢谢!

Edit: 编辑:

So here is more of the code, what is in my role.services, and my JSON. 因此,这里有更多代码,我的role.services和JSON中的内容。

Here is my role.services 这是我的角色。

import {Injectable} from 'angular2/core';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import 'rxjs/Rx';

@Injectable()
export class RoleService {


    constructor(public http: Http) {

    }

    getRoles(){
        return this.http.get('http://localhost:3000/api/roles/')
            .map((response => response.json().data));
    }

    getEditRoles(id){
        return this.http.get('http://localhost:3000/api/roles/edit/' +id)
            .map((response => response.json()))
    }
}

And here is my JSON I am trying to capture. 这是我尝试捕获的JSON

{
  new_modules: [
    {
      MODULE_NAME: "user_roles"
    }
  ],
  modules: [
     {
      ROLE_ID: 6,
      MODULE_NAME: "roles",
      INSERT_ALLOWED_FLAG: "Y",
      UPDATE_ALLOWED_FLAG: "N",
      DELETE_ALLOWED_FLAG: "N",
      QUERY_ONLY: "Y"
    },
    {
      ROLE_ID: 6,
      MODULE_NAME: "test_mod",
      INSERT_ALLOWED_FLAG: "Y",
      UPDATE_ALLOWED_FLAG: "N",
      DELETE_ALLOWED_FLAG: "N",
      QUERY_ONLY: "N"
    },
    {
      ROLE_ID: 6,
      MODULE_NAME: "users",
      INSERT_ALLOWED_FLAG: "Y",
      UPDATE_ALLOWED_FLAG: "Y",
      DELETE_ALLOWED_FLAG: "Y",
      QUERY_ONLY: "Y"
    }
  ],
 data: [
    {
      ID: 6,
      ROLE_NAME: "Fire",
      DESCRIPTION: "Something hot",
      ACTIVE_FLAG: "Y"
    }
  ]
}

Referring to and crediting this answer it appears that you may be missing the 提及并归功于此答案 ,看来您可能会错过

.map(res => res.json())

for your Http request. 为您的Http请求。 Since your code snippets do not include the getEditRoles code in the RoleService I cannot confirm that this is in fact missing. 由于您的代码段不包括getEditRoles代码RoleService我无法确认这其实就是失踪。 But my suspicion is that you are using making the Http request and then returning it. 但是我怀疑您正在使用发出Http请求,然后返回它。 Your code for that method should be something like to following for it to work: 该方法的代码应类似于以下内容,以使其起作用:

public getEditRoles(id: number): Observable<any>{
    return this.http.get('some.url', {id: id})
               .map(res => res.json());
}

This will ensure that the Response object from the Http call gets converted into JSON for use in your component. 这将确保来自Http调用的Response对象被转换为JSON以在您的组件中使用。

Update 更新资料

With your updated question I now see some discrepancies in your code. 关于您更新的问题,我现在在您的代码中看到了一些差异。 In your component you have this line 在您的组件中,您有这条线

this.modules = role_edit.modules[0]

Which means that you are assigning the array modules the value of the first object in the modules array of your json. 这意味着您要为数组 modules分配json的modules数组中第一个对象的值。

So at this moment this.modules would have a value of 因此,此刻this.modules的值为

{
  ROLE_ID: 6,
  MODULE_NAME: "roles",
  INSERT_ALLOWED_FLAG: "Y",
  UPDATE_ALLOWED_FLAG: "N",
  DELETE_ALLOWED_FLAG: "N",
  QUERY_ONLY: "Y"
}

Which then you are attempting to iterate over, but as it is not an array, the framework errors. 然后,您尝试对其进行迭代,但是由于它不是数组,因此框架错误。

For this to work as you expect change 为了使它按您期望的那样工作

this.modules = role_edit.modules[0]

to

this.modules = role_edit.modules

Which will assign the entire array to this.modules instead of only the first item. 它将整个数组分配给this.modules而不是仅第一项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM