How do I declare a model class in my Angular 2 component using TypeScript?

后端 未结 8 695
野趣味
野趣味 2020-12-23 11:10

I am new to Angular 2 and TypeScript and I\'m trying to follow best practices.

Instead of using a simple JavaScript model ({ }), I\'m attempting to create a TypeScri

8条回答
  •  -上瘾入骨i
    2020-12-23 11:25

    my code is

        import { Component } from '@angular/core';
    
    class model {
      username : string;
      password : string;
    }
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    
    
    
    export class AppComponent {
    
     username : string;
     password : string;
      usermodel = new model();
    
      login(){
      if(this.usermodel.username == "admin"){
        alert("hi");
      }else{
        alert("bye");
        this.usermodel.username = "";
      }    
      }
    }
    

    and the html goes like this :

    
    

提交回复
热议问题