未捕获(承诺):TypeError:无法读取未定义的属性'nombre'

时间:2017-12-20 14:25:16

标签: angular typescript ionic3

我正在开发一个ionic3-app,并且在尝试添加表单中的学生时遇到以下错误:

错误错误:未捕获(在承诺中):TypeError:无法读取未定义的属性'nombre'

我试过“猫王”变量推杆?在数据绑定中但没有用。

如何引用de subject属性的输入?

每个主题都有“nombre”,“estudios”,“cuatrimestre”,“tipo”atributtes,所有字符串而不是cuatrimestre,这是一个数字

打字稿代码是:

 import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { SubjectProvider} from '../../providers/subject/subject';

@IonicPage()
@Component({
  selector: 'page-addsubject',
  templateUrl: 'addsubject.html',
})
export class AddsubjectPage {

  subject: { nombre:'', estudios: '', cuatrimestre: '', tipo: ''};

  constructor(public navCtrl: NavController, public navParams: NavParams, public subjectRest: SubjectProvider) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad AddsubjectPage');
  }

  saveSubject() {
    this.subjectRest.saveSubject(this.subject).then((result) => {
     //const id = result['_id'];
      //this.router.navigate(['/subject-details', id]);
    }, (err) => {
      console.log(err);
    });
  }

}

和相应的html代码是:

<ion-header>

  <ion-navbar>
    <ion-title>Añadir asignatura</ion-title>
  </ion-navbar>

</ion-header>

<ion-content padding>
  <h2>Añadir Asignatura</h2>
  <form (ngSubmit)="saveSubject()">
    <ion-item>
      <ion-label>Nombre</ion-label>
      <ion-input type="text" required [(ngModel)]="subject.nombre" name="nombre"></ion-input>
    </ion-item>
    <ion-item>
      <ion-label>Nombre</ion-label>
      <ion-input type="text" required [(ngModel)]="subject.estudios" name="estudios"></ion-input>
    </ion-item>
    <ion-item>
      <ion-label>Cuatrimestre</ion-label>
      <ion-input type="number" required [(ngModel)]="subject.cuatrimestre" name="cuatrimestre"></ion-input>
    </ion-item>
    <ion-item>
      <ion-label>Tipo</ion-label>
      <ion-select type="text"  required [(ngModel)]="subject.tipo" name="tipo">
        <ion-option value="Obligatoria">Obligatoria</ion-option>
        <ion-option value="Optativa">Optativa</ion-option>
      </ion-select>
    </ion-item>
    <button ion-button type="submit" block>Añadir asignatura</button>
  </form>
</ion-content>

1 个答案:

答案 0 :(得分:-1)

主题变量初始值未定义。在构造函数中给它一个值。

<?php

namespace DummyNamespace;

use Illuminate\Http\Request;
use DummyRootNamespaceHttp\Controllers\Controller;

class %%ModelName%% extends Controller
{
   /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function admin_index()
    {
        $data=$this->%%ModelName%%->find('all');
            $this->set('%%ModelName%%s',$data);
    }


    public function admin_add()
    {
        if($this->request->is('%%ModelName%%'))
        {
            $this->%%ModelName%%->create();
            if($this->%%ModelName%%->save($this->request->data))
            {
                $this->Session->setFlash('The %%ModelName%% has been created.');
                return $this->redirect(array('action'=>'index'));
            }
            else
            {
                $this->Session->setFlash("The %%ModelName%% haven't been created!");
            }
        }   
        $this->set('topics',$this->%%ModelName%%->Topic->find('list'));
    }


    public function admin_view($id)
    {
        $data=$this->%%ModelName%%->findById($id);
        $this->set('%%ModelName%%',$data);
    }


    public function admin_edit($id)
    {
        $data=$this->%%ModelName%%->findById($id);
        if($this->request->is(array('%%ModelName%%','put')))
        {
            $this->%%ModelName%%->id=$id;
            if($this->%%ModelName%%->save($this->request->data))
            {
                $this->Session->setFlash('The %%ModelName%% has been edited.');
                return $this->redirect(array('action'=>'index'));
            }
            else
            {
                $this->Session->setFlash("The %%ModelName%% haven't been edited!");
            }
        }
        $this->request->data=$data;
        $this->set('topics',$this->%%ModelName%%->Topic->find('list'));
    }


    public function admin_delete($id)
    {
        $data=$this->%%ModelName%%->findById($id);
        if($this->request->is(array('%%ModelName%%','put')))
        {
            $this->%%ModelName%%->id=$id;
            if($this->%%ModelName%%->delete())
            {
                $this->Session->setFlash('The %%ModelName%% has been deleted.');
                return $this->redirect(array('action'=>'index'));
            }
            else
            {
                $this->Session->setFlash("The %%ModelName%% haven't been deleted!");
            }
        }

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}