简体   繁体   English

Angular2 / Typescript对象和属性绑定到模板

[英]Angular2/Typescript object and property binding to template

Note: I want to know correct way or something more 注意:我想知道正确的方法或更多
_____________________________________________________________________ _____________________________________________________________________
ANGULAR1 ANGULAR1

In angular1 , we could do something like below. angular1 ,我们可以执行以下操作。

$scope.user={};
$scope.user.name="micronyks";
$scope.user.age="27";

or directly like 或直接喜欢

$scope.user={name:"micronyks",age:"27"}

Binding to template looks like, 绑定到模板的样子,

<div>My name is {{user.name}} & I'm {{user.age}} year(s) old. </div>


______________________________________________________________________ ______________________________________________________________________
ANGULAR2 ANGULAR2

In angular2 with Typescript , 在与打字稿 angular2,

models.ts models.ts

export class LoginModel()
{
  private name:string;
  private age:number;
}

app.ts app.ts

import {LoginModel} from '../models.ts';

user=new LoginModel()
constructor()
{
   this.user.name="micronyks"
   this.user.age="27"
}

app.html app.html

<div>My name is {{user.name}} & I'm {{user.age}} year(s) old. </div <div>My name is {{user.name}} & I'm {{user.age}} year(s) old. </div > <div>My name is {{user.name}} & I'm {{user.age}} year(s) old. </div >

Can we directly create an object with property(ies) with values? 我们可以直接创建具有值的属性的对象吗?

I know TypeScript is mainly concerned with type . 我知道TypeScript主要与type有关。

But would like to know if something else is there and if someone know something into deep. 但想知道是否还有其他东西,以及是否有人知道某些东西。

If you want to show something you can use this plunker: Plunker 如果您想展示一些东西,您可以使用这个塞子: Plunker

You can use also use literal objects in Angular2 like Angular1. 您还可以在Angular2中使用文字对象,例如Angular1。 Since Angular2 can be used with TypeScript, you can leverage its strong types. 由于Angular2可与TypeScript一起使用,因此您可以利用其强类型。 This way you can specify the types for properties and parameters. 这样,您可以指定属性和参数的类型。 Checks aren't now possible and this allows to get type errors if uncorrect types are used. 现在无法进行检查,如果使用了不正确的类型,这将导致类型错误。

import {LoginModel} from '../models.ts';

export class Test {
  user:LoginModel = new LoginModel()

  constructor() {
    this.user.name = 'micronyks';
    this.user.age = 27;

    this.someMethod(this.user);
  }

  someMethod(user:LoginModel) {
    // ...
  }
}

Another point is related to dependency injection since Angular2 can rely on type to know which instance to inject... Otherwise you need to use @Inject . 另一点与依赖注入有关,因为Angular2可以依靠类型来知道要注入哪个实例...否则,您需要使用@Inject

There are also impacts within your IDE. 在您的IDE中也有影响。 Since you can have completion if you define types instead of no type or any . 因为如果定义类型而不是没有类型或any类型,就可以完成。

In fact, it's even possible to mix things: 实际上,甚至可以混合一些东西:

interface INumbersOnly {
  [key: string]: number;
}

var x: INumbersOnly = {
  num: 1, // works fine
  str: 'x' // will give a type error
};

So I would say that you're free to do what you want ;-) 所以我想说,您可以自由地做自己想做的事;-)

See this question for more details: 有关详细信息,请参阅此问题:

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

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