景孟勋 發表於 2019-11-4 16:56:00

angular的基本使用

<p>1、在组件中的component.ts文件中可以直接定义属性和它的值,在前端html页面中,通过<span style="color: rgba(255, 0, 0, 1)">{{title}}</span>的方式来引用</p>
<p><span style="color: rgba(255, 0, 0, 1)">public title: any = 'username'</span> // any 表示可以为任何类型的值&nbsp; 如果为其他的比如string/boolean则代表只能为这种类型值&nbsp; &nbsp;</p>
<p>定义属性的三种方式&nbsp; 1、public:公有属性,可以在类里面使用,也可以在外面使用&nbsp; &nbsp;//前面什么都不写则默认为public</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2、protected:保护类型,只有在当前类和它的子类中才可以访问</p>
<p>          3、private:私有属性,只有在当前类中才可以使用</p>
<p>在构造函数中,如果需要改变或者引用该数据时,通过<span style="color: rgba(255, 0, 0, 1)">this.title</span>即可获取</p>
<p>2、当需要给某个标签的属性赋值为定义的属性的值,通过<span style="color: rgba(255, 0, 0, 1)">[属性名]=属性值</span>的方式来&nbsp; &nbsp;vue则为 <span style="color: rgba(255, 0, 0, 1)">:属性名=属性值</span></p>
<p>&nbsp;</p>
<pre><span style="font-size: 14px; color: rgba(255, 0, 0, 1)"> &lt;h3 [属性名]="title"&gt;angular定义属性名&lt;/h3&gt;</span></pre>
<p>&nbsp;</p>
<p><span style="color: rgba(0, 0, 0, 1)">3、解析html代码</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  &lt;div <span style="color: rgba(255, 0, 0, 1)">="content"</span>&gt;&lt;/div&gt;&nbsp; &nbsp;vue则为<span style="color: rgba(255, 0, 0, 1)">v-html=""</span></span></p>
<p><span style="color: rgba(0, 0, 0, 1)">4、for循环&nbsp; &nbsp; vue循环-v-for</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 14px">&lt;ul&gt;
&lt;li <span style="color: rgba(255, 0, 0, 1)">*ngFor="let item of list;let key = index"</span>&gt; // key为索引<span style="color: rgba(0, 0, 0, 1)">
    {{item}} ---{{key}}
</span>&lt;/li&gt;
&lt;/ul&gt;</span></pre>
</div>
<p>5、引入静态资源中的图片</p>
<p>  &lt;img&nbsp; src="assets/imgurl" /&gt;既可获取</p>
<p>6、<span style="color: rgba(255, 0, 0, 1)">*ngIf 等同与v-if&nbsp; &nbsp;if条件判断&nbsp; &nbsp; &nbsp; angular没有else</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">7、switch的使用</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 14px">&lt;span ="switchNumber"&gt;
&lt;p *ngSwitchCase="1"&gt;收获1&lt;/p&gt;
&lt;p *ngSwitchCase="2"&gt;收获2&lt;/p&gt;
&lt;p *ngSwitchCase="3"&gt;收获3&lt;/p&gt;
&lt;p *ngSwitchCase="4"&gt;收获4&lt;/p&gt;
&lt;/span&gt;</span></pre>
</div>
<p>8、动态定义class</p>
<div class="cnblogs_code">
<pre><span style="font-size: 14px">&lt;span <span style="color: rgba(255, 0, 0, 1)">="{'red':true,'blue':false}"</span>&gt;1111111111&lt;/span&gt;
&lt;span <span style="color: rgba(255, 0, 0, 1)">="{'red':false,'blue':true}"</span>&gt;1111111111&lt;/span&gt;</span></pre>
</div>
<p>9、动态定义style</p>
<div class="cnblogs_code">
<pre><span style="font-size: 14px; color: rgba(0, 0, 0, 1)">&lt;span <span style="color: rgba(255, 0, 0, 1)">="{'color':'yellow'}"</span>&gt;3333333&lt;/span&gt;</span></pre>
</div>
<p>10、方法的调用</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(255, 0, 0, 1); font-size: 14px">&lt;button (click)="run()"&gt;点击执行事件&lt;/button&gt;将方法定义在ts文件中,即可直接调用</span></pre>
</div>
<p>11、获取DOM节点并操作它的方式</p>
<div class="cnblogs_code">
<pre><span style="font-size: 14px"><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 监听事件的地方</span>
&lt;div <span style="color: rgba(255, 0, 0, 1)">(click)="getDom($event)</span>"&gt;
66666666666666
&lt;/div&gt;

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 执行事件的地方,</span>
<span style="color: rgba(0, 0, 0, 1)"> getDom(e) {
<span style="color: rgba(255, 0, 0, 1)">const dom </span></span><span style="color: rgba(255, 0, 0, 1)">= e.target;
    dom.style.color = 'red'</span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(255, 0, 0, 1)">;</span>
}</span></span></pre>
</div>
<p>12、MVVM双向数据绑定(只针对表单)&nbsp; 首先需要在该文件下引入FormModule,并且在import中声明一次。</p>
<p><img src="https://img2018.cnblogs.com/i-beta/1631381/201911/1631381-20191104165325309-399378360.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="font-size: 14px">&lt;input type="text" <span style="color: rgba(255, 0, 0, 1)">[(ngModel)]="keywords</span>"&gt;   此时即可实现双向数据绑定<span style="color: rgba(0, 0, 0, 1)">
{{keywords}}</span></span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/cazj/p/11792999.html
頁: [1]
查看完整版本: angular的基本使用