阡阡 發表於 2020-8-10 10:57:00

Angular

<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; text-align: center" align="center"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 24pt">Angular</span></strong></p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;<img src="https://img2020.cnblogs.com/blog/1172834/202009/1172834-20200924115306920-841095737.jpg" alt="" loading="lazy" style="display: block; margin-left: auto; margin-right: auto"></span></p>
<p>&nbsp;</p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt"></span></p>
<p>&nbsp;</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">前言</span></p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">简介</span></p>
<p class="MsoNormal" style="margin-left: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">Angular简称</span></p>
<p class="MsoNormal" style="margin-left: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">四个特性</span></p>
<p class="MsoNormal" style="margin-left: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">优点</span></p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">初尝试</span></p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">常用介绍</span></p>
<p class="MsoNormal" style="margin-left: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">指令</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">说明</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-app</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-bind</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-repeat</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-class</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-show/ng-hide/ng-if:true/false</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-href/ng-src</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-switch</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-checked/ng-disabled/ng-readonly/ng-selected</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">常用事件指令</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">自定义指令</span></p>
<p class="MsoNormal" style="margin-left: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">控制器</span></p>
<p class="MsoNormal" style="margin-left: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">表达式</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">闪烁问题</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">表达式可承载的内容</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">双向数据绑定</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">$scope</span></p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 21pt">前言</span></strong></p>
<p class="MsoNormal" style="text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">单一页面程序【</span>SPA】体会:现在请你打开网易云的官网https://music.163.com,播放一首你最喜欢的歌曲,然后点击一下”我的音乐“,不知道这个过程你是否会想到这个问题:唉?我都点击一个资源了,页面并没有跳转也没有任何刷新的迹象,页面的头部没有改变,更重要的是你最喜欢的歌依旧没有任何停顿的在播放。不用我说你可能觉得这很简单,这不就是用ajax去做的吗?这有什么难的!没错,这个过程确实是用ajax去做的,那你该问这个Angular有个毛关系?关系不关系我们接着看!如果你足够细心,会发现当你点击“我的音乐”时,浏览器是没有刷新的,但是浏览器的地址栏却发生了一些细微的变化,在地址栏中出现了像是锚点的东西,不过我们的锚点不是常常指向一个dom元素的id吗?细看这个锚点后面的东西有些像一个路径,下面我们来扯一扯window对象的一个锚点切换事件你大概就略有所悟了。</span></p>
<p class="MsoNormal"></p>
<div class="cnblogs_code">
<pre>&lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">
    (</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (window) {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 每当锚点改变触发以下事件</span>
      window.addEventListener('hashchange', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (e) {
            console.log(e);
            console.log(window.location);
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> hash =<span style="color: rgba(0, 0, 0, 1)"> window.location.hash;
            
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 网易锚点后的链接</span>
            <span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (hash) {
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> '#/my/'<span style="color: rgba(0, 0, 0, 1)">:
                  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ajax do somethings</span>
                  <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> '#/friend'<span style="color: rgba(0, 0, 0, 1)">:
                  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ajax do somethings</span>
                  <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> '#/download'<span style="color: rgba(0, 0, 0, 1)">:
                  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ajax do somethings</span>
                  <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
            }
      })
    })(window)
</span>&lt;/script&gt;</pre>
</div>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">说白了,还是使用</span>ajax去请求后台,只不过是换了一种方式,使用锚点的方式触发了。</span></p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 21pt">简介</span></strong></p>
<p class="MsoNormal" style="text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">来自百度百科的介绍:</span><span style="text-decoration: underline"><span style="font-family: &quot;Times New Roman&quot;; color: rgba(0, 56, 132, 1); font-size: 10.5pt">https://baike.baidu.com/item/SPA/17536313</span></span></p>
<p class="MsoNormal" style="text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">中文官网:</span><span style="text-decoration: underline"><span style="font-family: &quot;Times New Roman&quot;; color: rgba(0, 56, 132, 1); font-size: 10.5pt">https://www.angular.cn/</span></span></p>
<p class="MsoNormal" style="text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; color: rgba(57, 57, 57, 1); font-size: 10.5pt">API:</span><span style="text-decoration: underline"><span style="font-family: &quot;Times New Roman&quot;; color: rgba(0, 56, 132, 1); font-size: 10.5pt">https://docs.angularjs.org/api</span></span></p>
<p class="MsoNormal" style="text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; color: rgba(57, 57, 57, 1); font-size: 10.5pt">一个测试不同技术的模板:</span><span style="text-decoration: underline"><span style="font-family: &quot;Times New Roman&quot;; color: rgba(0, 56, 132, 1); font-size: 10.5pt">https://github.com/tastejs/todomvc-app-template</span></span></p>
<p class="MsoNormal" style="text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; text-indent: 21pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 15pt">Angular简称</span></strong></p>
<p class="MsoNormal" style="text-indent: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng</span></p>
<p class="MsoNormal" style="text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; text-indent: 21pt"><strong><span style="font-family: &quot;Times New Roman&quot;; color: rgba(57, 57, 57, 1); font-size: 15pt">四个特性</span></strong></p>
<p class="MsoNormal" style="margin-left: 21pt; text-indent: 42pt; mso-list: l0 level1 lfo1"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">MVC</span></p>
<p class="MsoNormal" style="margin-left: 21pt; text-indent: 42pt; mso-list: l0 level1 lfo1"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">模块化</span></p>
<p class="MsoNormal" style="margin-left: 21pt; text-indent: 42pt; mso-list: l0 level1 lfo1"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">自动化双向数据绑定</span></p>
<p class="MsoNormal" style="margin-left: 21pt; text-indent: 42pt; mso-list: l0 level1 lfo1"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">指令系统</span></p>
<p class="MsoNormal" style="margin-left: 21pt; text-indent: 42pt"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; margin-left: 21pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 15pt">优点</span></strong></p>
<p class="MsoNormal" style="margin-left: 60px"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">Angular最大程度的减少了页面上的DOM操作;</span></p>
<p class="MsoNormal" style="margin-left: 60px"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">让</span>JavaScript中专注业务逻辑的代码;</span></p>
<p class="MsoNormal" style="margin-left: 60px"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">通过简单的指令结合页面结构与逻辑数据;</span></p>
<p class="MsoNormal" style="margin-left: 60px"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">通过自定义指令实现组件化编程;</span></p>
<p class="MsoNormal" style="margin-left: 60px"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">代码结构更合理;</span></p>
<p class="MsoNormal" style="margin-left: 60px"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">维护成本更低;</span></p>
<p class="MsoNormal" style="margin-left: 60px"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">Angular解放了传统JavaScript中频繁的DOM操作;</span></p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0"><strong><span style="font-family: &quot;Times New Roman&quot;; color: rgba(57, 57, 57, 1); font-size: 21pt">初尝试</span></strong></p>
<p class="MsoNormal" style="text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; color: rgba(57, 57, 57, 1); font-size: 10.5pt">官网地址:</span><span style="text-decoration: underline"><span style="font-family: &quot;Times New Roman&quot;; color: rgba(0, 56, 132, 1); font-size: 10.5pt">https://docs.angularjs.org/tutorial/step_00</span></span></p>
<p class="MsoNormal" style="text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; color: rgba(57, 57, 57, 1); font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">插件:</span>AngularJS Batarang自己去goole商店下载,可用来查看Angular一些数据渲染</span></p>
<p class="MsoNormal" style="text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal" style="text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; color: rgba(57, 57, 57, 1); font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">首先:所有需要</span>ng管理的代码必须被包裹在一个有ng-app指令的元素中,ng-app是ng的入口,表示当前元素的所有指令都会被angular管理(对每一个指令进行分析和操作)。</span></p>
<div class="cnblogs_code">
<pre>&lt;body&gt;
&lt;!--所有需要ng管理的代码必须被包裹在一个有ng-app指令的元素中,ng-<span style="color: rgba(0, 0, 0, 1)">app是ng的入口,
表示当前元素的所有指令都会被angular管理(对每一个指令进行分析和操作)</span>--&gt;
&lt;div ng-app ng-init="_name='毛毛'"&gt; &lt;!--ng-init表示初始化一个默认值--&gt;
    &lt;!--在ng中定义一个模型--&gt;
    &lt;input type="text" ng-model="name"&gt;
    &lt;!--在ng中使用定义的模型--&gt;
    &lt;p&gt;你好,{{name}}&lt;/p&gt;
&lt;/div&gt;
&lt;script src="../node_modules/angular/angular.js"&gt;&lt;/script&gt;
&lt;/body&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">代码解析:</span></p>
<p class="MsoNormal" style="text-indent: 21pt; mso-list: l0 level1 lfo1"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">当网页加载完毕,</span>AngularJS自动开始执行;</span></p>
<p class="MsoNormal" style="text-indent: 21pt; mso-list: l0 level1 lfo1"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">HTML页面中ng-xxx的属性称之为指令(Directive);</span></p>
<p class="MsoNormal" style="text-indent: 21pt; mso-list: l0 level1 lfo1"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-app指令告诉AngularJS,&lt;div&gt;元素是AngularJS程序管理的边界;</span></p>
<p class="MsoNormal" style="text-indent: 21pt; mso-list: l0 level1 lfo1"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-model:是双向数据绑定的指令,效果就是将当前元素的value属性和模型中的name建立绑定关系。</span></p>
<p class="MsoNormal" style="text-indent: 21pt; mso-list: l0 level1 lfo1"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">{{name}}表达式就是把应用程序变量name绑定到某个段落的innerHTML</span></p>
<p class="MsoNormal"><img src="https://img2020.cnblogs.com/blog/1172834/202008/1172834-20200810105149846-1504681689.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>&lt;!--如果ng-app中有模块名,则表示我这个div将由myApp这个模块管理,否则由angular管理--&gt;
&lt;!--ng-controller中指明我这个模块将由AppController控制--&gt;
&lt;div ng-app="myApp" ng-controller="AppController"&gt;
    &lt;!--在ng中定义一个模型--&gt;
    &lt;input type="text" ng-model="user.name"&gt;
    &lt;!--在ng中使用定义的模型--&gt;
    &lt;p&gt;你好,{{user.name}}&lt;/p&gt;
    &lt;button ng-click="show()"&gt;点击&lt;/button&gt;
&lt;/div&gt;
&lt;script src="../node_modules/angular/angular.js"&gt;&lt;/script&gt;
&lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">
    (</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
      </span><span style="color: rgba(0, 0, 255, 1)">var</span> myApp = angular.module("myApp",[]);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第二个参数数组表示可能引用的模块</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 存在不能混淆压缩的问题</span>
      myApp.controller("AppController",<span style="color: rgba(0, 0, 255, 1)">function</span>($scope) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $scope这个参数名称固定!代表上图中存储区块对象</span>
            $scope.user =<span style="color: rgba(0, 0, 0, 1)"> {};
            $scope.user.name </span>= '毛毛'<span style="color: rgba(0, 0, 0, 1)">;
            $scope.show </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
                console.log($scope.user.name);
            }
      });
    })()
</span>&lt;/script&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">注册控制器存在的问题:在上面说了</span>$scope是一个固定写死的参数,一旦更改名称,angular将不能识别注入!这就存在一个问题就是js压缩问题,我们都知道生产环境中可以使用未压缩的js文件,但是上线常常使用标识符混淆的压缩方式压缩代码,一旦使用标识符混淆,$scope就会被改变,angular又不能注入了。angular为了解决这个问题,声明了一种标准的写法:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 标准写法:</span>
myApp.controller("AppController",['$scope',<span style="color: rgba(0, 0, 255, 1)">function</span>(a) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 字符串'$scope'固定写法</span>
            a.user =<span style="color: rgba(0, 0, 0, 1)"> {};
            a.user.name </span>= '毛毛'<span style="color: rgba(0, 0, 0, 1)">;
            a.show </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
                console.log(a.user.name);
            }
      }]);</span></pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">使用数组的方式,要求数组的最后一个元素是一个</span>function,之前的元素对应该function中的参数,由于数组中存储的function参数是字符串类型而不是一个变量,就不会存在混淆压缩被替换的问题。</span></p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 21pt">常用介绍</span></strong></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; text-indent: 21pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 15pt">指令</span></strong></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; text-indent: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">说明</span></strong></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 63pt; mso-list: l0 level1 lfo1"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-xxx的属性本身并不是标准中定义的属性,很多情况下语法检验(W3C)是无法通过的</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 63pt; mso-list: l0 level1 lfo1"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">HTML5允许扩展的(自制的)属性,以data-开头,在AngularJS中可以使用data-ng-xx来让网页对HTML5有效,这和直接在元素中使用ng-xx效果相同</span></p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; margin-left: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">ng-app</span></strong></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">标记范围尽可能小,为了性能</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">最好只出现一个</span>ng-app,如果出现多个ng-app就需要手动引导,或模块带入方式实现</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; margin-left: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">ng-bind</span></strong></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">在下面讲到表达式的时候,在表达式中存在一个闪烁问题,解决这个问题我们还可以使用</span>ng-bind中绑定数据,而不是直接在标签文本内部使用。</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">你可以在其中绑定一些基本的数据,但是并不建议你去绑定</span>html,因为这将带来不安全的操作,可能存在一些脚本攻击【假如你绑定的是某个用户评论的内容,该内容是一段js脚本用来获取你本地cookie信息模拟你登陆,窃取一些东西】。所以默认的你在ng-bind中所有的文本标签&lt;,&gt;将会被替换为&lt;&gt;,防止你绑定的数据是一个脚本。如果你非要去绑定一个标签内容,并想呈现标签的效果,你就要使用ng-bind-html,使用该标签你要求你使用angular提供的一种校验库sanitize,用以校验你绑定的内容是否安全。【即使这样,还是不推荐你去绑定html】</span></p>
<div class="cnblogs_code">
<pre>&lt;body ng-app ng-init="h='shif'"&gt;
    &lt;p&gt;{{h}}&lt;/p&gt;
    &lt;p ng-bind="h"&gt;&lt;/p&gt;&lt;!--不会出现闪烁问题--&gt;
    &lt;script src="../../node_modules/angular/angular.js"&gt;&lt;/script&gt;
&lt;/body&gt;</pre>
</div>
<div class="cnblogs_code">
<pre>&lt;body ng-app="myApp" ng-init="exam='&lt;h1&gt;shif&lt;/h1&gt;'"&gt;
    &lt;p ng-bind-html="exam"&gt;&lt;/p&gt;
    &lt;script src="../../node_modules/angular/angular.js"&gt;&lt;/script&gt;
    &lt;script src="../../node_modules/angular-sanitize/angular-sanitize.js"&gt;&lt;/script&gt;
    &lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">
      (</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> myApp = angular.module("myApp",["ngSanitize"<span style="color: rgba(0, 0, 0, 1)">])
      })();
    </span>&lt;/script&gt;
&lt;/body&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; margin-left: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">ng-repeat</span></strong></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">用来遍历一个数组重复创建当前元素。</span></p>
<div class="cnblogs_code">
<pre>&lt;body ng-app="myApp"&gt;
&lt;ul ng-controller="ULController"&gt;
    &lt;li ng-repeat="item in data"&gt;
      &lt;span&gt;id:{{$id}}&lt;/span&gt;
      &lt;span&gt;index:{{$index}}&lt;/span&gt;
      &lt;span&gt;first:{{$first}}&lt;/span&gt;
      &lt;span&gt;last:{{$last}}&lt;/span&gt;
      &lt;span&gt;middle:{{$middle}}&lt;/span&gt;
      &lt;span&gt;even:{{$even}}&lt;/span&gt;
      &lt;span&gt;odd:{{$odd}}&lt;/span&gt;
      &lt;span&gt;{{item.name}}&lt;/span&gt;
    &lt;/li&gt;
&lt;/ul&gt;
&lt;script src="../../node_modules/angular/angular.js"&gt;&lt;/script&gt;
&lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">
    (</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
      angular.module(</span>'myApp', []).controller('ULController', ['$scope', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> ($scope) {
            $scope.data </span>= [{name: '毛毛'}, {name: '吉吉'}, {name: '咪咪'}, {name: '可可'<span style="color: rgba(0, 0, 0, 1)">}];
      }])
    })();
</span>&lt;/script&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">如果遍历的元素存在重复的,则需要</span>track by 一个不重复的量:</span></p>
<div class="cnblogs_code">
<pre>&lt;li ng-repeat="item in data track by $index"&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; margin-left: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">ng-class</span></strong></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-class中可以写入一个对象,如下例中对象的key值表示现有的css样式属性名,对象的value为true或者false,表示是否起作用。ng-class还可以绑定model。</span></p>
<div class="cnblogs_code">
<pre>&lt;style&gt;<span style="color: rgba(0, 0, 0, 1)">
      .red {
            color: red;
      }

      .green {
            color: yellow;
      }
    </span>&lt;/style&gt;
&lt;/head&gt;
&lt;body ng-app="myApp" ng-init="nnm='red'"&gt;
&lt;div ng-controller="ULController"&gt;
    &lt;ul&gt;
      &lt;li ng-repeat="item in data"&gt;&lt;!--隔行换色--&gt;
            &lt;span class="{{$even?'red':'green'}}"&gt;{{item.name}}&lt;/span&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul&gt;
      &lt;li ng-repeat="item in data"&gt;&lt;!--隔行换色--&gt;
            &lt;span ng-class="{red:$even,green:$odd}"&gt;{{item.name}}&lt;/span&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;span ng-class="nnm"&gt;{{ 10+20 }}&lt;/span&gt; &lt;!--还可以绑定一个模型--&gt;
&lt;/div&gt;

&lt;script src="../../node_modules/angular/angular.js"&gt;&lt;/script&gt;
&lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">
    (</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
      angular.module(</span>'myApp', []).controller('ULController', ['$scope', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> ($scope) {
            $scope.data </span>= [{name: '毛毛'}, {name: '吉吉'}, {name: '咪咪'}, {name: '可可'<span style="color: rgba(0, 0, 0, 1)">}];
      }])
    })();
</span>&lt;/script&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; margin-left: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">ng-show/ng-hide/ng-if:true/false</span></strong></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-show:决定是否显示</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-hide:决定是否隐藏</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-if:决定是否显示[不显示就删除]</span></p>
<div class="cnblogs_code">
<pre>&lt;body ng-app="myApp" ng-init="nnm='red'"&gt;
&lt;div ng-controller="ULController"&gt;
    &lt;div class="hong" ng-show="show"&gt;&lt;/div&gt;
    &lt;div class="hong" ng-hide="show"&gt;&lt;/div&gt;
    &lt;div class="hong" ng-<span style="color: rgba(0, 0, 255, 1)">if</span>="if"&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;script src="../../node_modules/angular/angular.js"&gt;&lt;/script&gt;
&lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">
    (</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
      angular.module(</span>'myApp', []).controller('ULController', ['$scope', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> ($scope) {
            $scope.show </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
            $scope.hide </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
            $scope.</span><span style="color: rgba(0, 0, 255, 1)">if</span> = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
      }])
    })();
</span>&lt;/script&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; margin-left: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">ng-href/ng-src</span></strong></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-href/ng-src指令用于解决当链接类型的数据绑定时造成的加载BUG</span></p>
<p class="MsoNormal"></p>
<div class="cnblogs_code">
<pre>&lt;body ng-app ng-init="url = '../images/123.png'"&gt;
&lt;img src="{{url}}" alt=""&gt; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 在angular没起作用之前就开始发送了请求,所有控制台会报一个错</span>
&lt;img ng-src="{{url}}" alt=""&gt;

&lt;script src="../../node_modules/angular/angular.js"&gt;&lt;/script&gt;
&lt;/body&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; margin-left: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">ng-switch</span></strong></p>
<div class="cnblogs_code">
<pre>&lt;div ng-app="myApp" ng-controller="AppController"&gt;
    &lt;div ng-repeat="item in exam"&gt;
      &lt;div ng-<span style="color: rgba(0, 0, 255, 1)">switch</span>="item.name"&gt;
            &lt;p ng-<span style="color: rgba(0, 0, 255, 1)">switch</span>-when="Liu"&gt;刘&lt;/p&gt;
            &lt;p ng-<span style="color: rgba(0, 0, 255, 1)">switch</span>-when="Li"&gt;李&lt;/p&gt;
            &lt;p ng-<span style="color: rgba(0, 0, 255, 1)">switch</span>-when="Wang"&gt;王&lt;/p&gt;
            &lt;p ng-<span style="color: rgba(0, 0, 255, 1)">switch</span>-<span style="color: rgba(0, 0, 255, 1)">default</span>&gt;赵&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;script src="../../node_modules/angular/angular.js"&gt;&lt;/script&gt;
&lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">
    (</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
      angular.module(</span>'myApp',[]).controller('AppController',['$scope',<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> ($scope) {
            $scope.exam </span>= [{name:'Li'},{name:'Liu'},{name:'Zhao'},{name:'Wang'<span style="color: rgba(0, 0, 0, 1)">}]
      }]);
    })();
</span>&lt;/script&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; margin-left: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">ng-checked/ng-disabled/ng-readonly/ng-selected</span></strong></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-checked:单选/复选是否选中</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-disable:是否禁用</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-readonly:是否只读</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-selected:是否选中</span></p>
<div class="cnblogs_code">
<pre>&lt;!-- 双向绑定的,任意一个选中则全部选中 --&gt;
&lt;input type="checkbox" ng-model="checked"&gt;&lt;br/&gt;
&lt;input type="checkbox" ng-model="checked"&gt;
&lt;input type="checkbox" ng-model="checked"&gt;
&lt;input type="checkbox" ng-model="checked"&gt;</pre>
</div>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>&lt;!-- ng-model双向绑定的,ng-checked时模型到视图的绑定(单项),第一个选中全选中,其他选中只选中自己 --&gt;
&lt;input type="checkbox" ng-model="checked"&gt;&lt;br/&gt;
&lt;input type="checkbox" ng-checked="checked"&gt;
&lt;input type="checkbox" ng-checked="checked"&gt;
&lt;input type="checkbox" ng-checked="checked"&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; margin-left: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">常用事件指令</span></strong></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-blur:失去焦点</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-change:发生改变</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-copy:拷贝完成</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-click:单击</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-dbclick:双击</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-focus:得到焦点</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-blur:失去焦点</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">ng-submit:表单提交</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; margin-left: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">自定义指令</span></strong></p>
<div class="cnblogs_code">
<pre>&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;自定义指令&lt;/title&gt;
    &lt;link rel="stylesheet" href="../../node_modules/bootstrap/dist/css/bootstrap.css"&gt;
&lt;/head&gt;
&lt;body ng-app="myApp" ng-init="nnm='red'"&gt;
&lt;div ng-controller="ULController"&gt;
    &lt;breadcrumb data="{{data}}"&gt;&lt;/breadcrumb&gt;
&lt;/div&gt;

&lt;script src="../../node_modules/angular/angular.js"&gt;&lt;/script&gt;
&lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">
    (</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
      </span><span style="color: rgba(0, 0, 255, 1)">var</span> myApp = angular.module('myApp'<span style="color: rgba(0, 0, 0, 1)">, []);
      myApp.controller(</span>'ULController', ['$scope', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> ($scope) {
            $scope.data </span>= ['blogs','home','doc','index'<span style="color: rgba(0, 0, 0, 1)">];
      }]);
      </span><span style="color: rgba(0, 128, 0, 1)">/*</span><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)">
      myApp.directive(</span>'breadcrumb',[<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
                scope:{
                  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> data:'@' // 不能直接引用到该元素的data属性,因为直接拿到的是一个字符串,不是对象</span>
<span style="color: rgba(0, 0, 0, 1)">                },
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> template:'&lt;h1&gt;xasx&lt;/h1&gt;',</span>
                templateUrl:'./template.html',<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 模板的地址</span>
                replace:<span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否是以替代的方式替换之前元素</span>
                link:<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (scope,element,attributes) {
                  scope.data </span>=<span style="color: rgba(0, 0, 0, 1)"> JSON.parse(attributes.data);
                  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log(scope.data);</span>
<span style="color: rgba(0, 0, 0, 1)">                }
            }
      }])
    })();
</span>&lt;/script&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin-left: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">上代码引用的</span>template.html:</span></p>
<div class="cnblogs_code">
<pre>&lt;nav aria-label="breadcrumb"&gt;
    &lt;ol class="breadcrumb"&gt;
      &lt;li class="breadcrumb-item" ng-repeat="item in data track by $index"&gt;
            &lt;a href="#" ng-<span style="color: rgba(0, 0, 255, 1)">if</span>="$last == false ? true:false"&gt;{{item}}&lt;/a&gt;
            &lt;span ng-<span style="color: rgba(0, 0, 255, 1)">if</span>="$last"&gt;{{item}}&lt;/span&gt;
      &lt;/li&gt;
    &lt;/ol&gt;
&lt;/nav&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; text-indent: 21pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 15pt">控制器</span></strong></p>
<p class="MsoNormal" style="text-indent: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">控制器的三大作用:</span></p>
<p class="MsoNormal" style="text-indent: 63pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">暴露数据</span></p>
<p class="MsoNormal" style="text-indent: 63pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">暴露行为</span></p>
<p class="MsoNormal" style="text-indent: 63pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">监视数据变化</span></p>
<div class="cnblogs_code">
<pre>&lt;body ng-app="Login"&gt;&lt;!--这是angular管理的一个模块--&gt;
&lt;table border="1" ng-controller="LoginController"&gt;&lt;!--该模块的这一部分dom由LoginController控制--&gt;
    &lt;tr&gt;
      &lt;td&gt;用户名:&lt;/td&gt;
      &lt;td&gt;&lt;input type="text" ng-model="user.name"&gt;&lt;/td&gt;&lt;!--双向绑定$scope中的数据模型--&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;密码:&lt;/td&gt;
      &lt;td&gt;&lt;input type="password" ng-model="user.password"&gt;&lt;/td&gt;&lt;!--双向绑定$scope中的数据模型--&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="button" value="登陆" ng-click="login()"&gt;&lt;/td&gt;&lt;!--绑定$scope中的方法--&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td colspan="2"&gt;{{message}}&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
&lt;script src="../node_modules/angular/angular.js"&gt;&lt;/script&gt;
&lt;script&gt;
    <span style="color: rgba(0, 0, 255, 1)">var</span> loginApp = angular.module("Login"<span style="color: rgba(0, 0, 0, 1)">,[]);
    loginApp.controller(</span>"LoginController",['$scope',<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> ($scope) {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据</span>
      $scope.user =<span style="color: rgba(0, 0, 0, 1)"> {
            name:</span>''<span style="color: rgba(0, 0, 0, 1)">,
            password:</span>''<span style="color: rgba(0, 0, 0, 1)">,
      };

      $scope.message </span>= ''<span style="color: rgba(0, 0, 0, 1)">;

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 行为数据</span>
      $scope.login = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ajax do somethings</span>
<span style="color: rgba(0, 0, 0, 1)">            console.log($scope.user);
      };

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 监视数据的行为,第一个参数为要监视的模型,第二个参数为一个function</span>
      $scope.$watch('user.name',<span style="color: rgba(0, 0, 255, 1)">function</span> (now,old) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> now表示模型的当前值,old表示模型的上一次值</span>
<span style="color: rgba(0, 0, 0, 1)">            console.log(now,old);
            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(now) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (now.length &lt; 7<span style="color: rgba(0, 0, 0, 1)">) {
                  $scope.message </span>= '输入的用户名不合法!'<span style="color: rgba(0, 0, 0, 1)">;
                } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                  $scope.message </span>= ''<span style="color: rgba(0, 0, 0, 1)">;
                }
            }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                $scope.message </span>= '请输入用户名!'<span style="color: rgba(0, 0, 0, 1)">;
            }
      })
    }])
</span>&lt;/script&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; text-indent: 21pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 15pt">表达式</span></strong></p>
<p class="MsoNormal" style="text-indent: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">表达式是单向数据绑定的。</span></p>
<p class="MsoNormal" style="text-indent: 42pt"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; text-indent: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">闪烁问题</span></strong></p>
<div class="cnblogs_code">
<pre>&lt;body&gt;
&lt;div ng-app ng-init="_name='毛毛'"&gt;
    &lt;input type="text" ng-model="_name"&gt;
    &lt;p class="ng-cloak"&gt;你好,{{_name}}&lt;/p&gt;
&lt;/div&gt;
&lt;script src="../node_modules/angular/angular.js"&gt;&lt;/script&gt;
&lt;/body&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">依旧是最开始的例子,在</span>goole里测试由于goole的缓存可能看不到,这个例子在火狐里面你就会发现,每次刷新时页面先出现“你好,{{_name}}”,然后又迅速的变成了“你好,毛毛”,查其原因,当然是由浏览器解析dom的顺序导致的,由于我们是在后面导出angular的,所以浏览器按照顺序解析会先出现“你好,{{_name}}”,然后当解析到angularJS时才将表达式里的内容替换掉的。当你把angularJS文件的引用放到最开始,就不会出现这个问题(如果你刷新的速度足够快,还是会出现闪烁问题)。</span></p>
<p class="MsoNormal" style="text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">Angular为了解决这个问题提供了一个属性标签ng-cloak,将存在表达式的标签上添加该属性,并且还要把angularJS文件的引用放在前面才行。</span></p>
<p class="MsoNormal" style="text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">当然还有另外一种解决方案使用</span>css方式去控制,我们依旧使用angularJS提供的指令标签ng-cloak,然后写一段css代码:</span></p>
<div class="cnblogs_code">
<pre>&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;Angular&lt;/title&gt;
    &lt;style&gt;<span style="color: rgba(0, 0, 0, 1)">
       {
            display: none;
      }
    </span>&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div ng-app ng-init="_name='毛毛'"&gt;
    &lt;input type="text" ng-model="_name"&gt;
    &lt;p ng-cloak&gt;你好,{{_name}}&lt;/p&gt;
&lt;/div&gt;
&lt;script src="../node_modules/angular/angular.js"&gt;&lt;/script&gt;
&lt;/body&gt;</pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal" style="text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">使用属性选择器先将该元素隐藏,等加载</span>angularJS时,由angularJS渲染表达式,最后将标签显现出来。</span></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; text-indent: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">表达式可承载的内容</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"> //数字

{{ </span>100 + 100<span style="color: rgba(0, 0, 0, 1)"> }}

//字符串

{{ </span>'hello' + 'world'<span style="color: rgba(0, 0, 0, 1)"> }}

//对象

{{ user.name }}

数组

{{ data[</span>0<span style="color: rgba(0, 0, 0, 1)">] }}

//三元表达式

{{ flag </span>== <span style="color: rgba(0, 0, 255, 1)">true</span> ? data : data }}<br></span></pre>
</div>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; text-indent: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">双向数据绑定</span></strong></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">两个方向的数据自动同步:</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">-模型($scope)发生变化自动同步到视图上</span></p>
<p class="MsoNormal" style="margin-left: 42pt; text-indent: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">-视图上的数据发生变化过后自动同步到模型上</span></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;</span></p>
<p class="MsoNormal" style="margin-top: 0; margin-bottom: 0; margin-left: 42pt"><strong><span style="font-family: &quot;Times New Roman&quot;; font-size: 12pt">$scope</span></strong></p>
<p class="MsoNormal" style="margin-left: 21pt; text-indent: 42pt"><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">$scope(上下文模型)</span></p>
<p class="MsoNormal" style="margin-left: 111px"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">视图和控制器之间的桥梁</span></p>
<p class="MsoNormal" style="margin-left: 111px"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">用于在视图和控制器之间传递数据</span></p>
<p class="MsoNormal" style="margin-left: 111px"><span style="font-family: &quot;Times New Roman&quot;; font-size: 10.5pt">&nbsp;</span><span style="mso-spacerun: 'yes'; font-family: &quot;Times New Roman&quot;; font-size: 10.5pt"><span style="font-family: &quot;Times New Roman&quot;">利用</span>$scope暴露数据模型</span></p>
<p class="MsoNormal"><span style="mso-spacerun: 'yes'; font-family: Calibri; font-size: 10.5pt">&nbsp;<img src="https://img2020.cnblogs.com/blog/1172834/202009/1172834-20200924115322446-261959425.gif" alt="" loading="lazy" style="display: block; margin-left: auto; margin-right: auto"></span></p>

</div>
<div id="MySignature" role="contentinfo">
    前进时,请别遗忘了身后的脚印。<br><br>
来源:https://www.cnblogs.com/liudaihuablogs/p/13468542.html
頁: [1]
查看完整版本: Angular