绑定数据
tip:所有的数据绑定,属性绑定,对象绑定,html绑定最好都要在对应的ts文件中进行,使用是在对应的html中
ng 中的是三个声明属性的方式
- public 共有(默认) 可以在这个类中使用,也可在类外使用
- protected 保护类型 只有在当前和他的子类中可以使用
- private 私有 只有在当前类才可以使用
定义数据类型:
any可以是任何数据类型,定义是什么类型后面就接什么数据类型
public username:any= “luck”;
public list:number = 3;
public name:string = “luck”;
定义对象:
public userInfo = {“username”:”rocky”,age:20}; 使用 {{userInfo.username}}
推荐使用的写法:
public userName:any = “luck”;
当定义一个数据不给其赋值,可以在ts中的(构造函数)constructor(){}中进行赋值
eg: public username:any;
constructor(){
this.any = “我就是在构造函数中给username赋的值”;
}
html中使用 {{username}};
获取值(简单方法)console.log(this.username); consloe.log 同样是在浏览器中进行查看
改变值(简单方法)this.username=“需要改变的值”;
以上两种方式需要在构造函数constructor中进行
模板中绑定属性:
1.ts中定义一个属性
public student:any=”she is student”;
2.html中使用
<div [title]=”student”></div>
模板中绑定html
1.ts中定义一个html标签
public content = “<h2>数据内容</h2>”;
2.html 中使用
连同html标签一起显示
<div>{{content}}</div>
只显示标签中的内容
<div ><span [innHTML]=”content” ></span></div>
html中的标签是可以加上class的
模板中可以进行简单的运算
1+2 = {{1+2}}