Page 78 - AngularJS权威教程
P. 78

58  第 9 章  内置指令


                 在下面的例子中,我们通过ng-init指令将someProperty的值设置为true。将some Property
             同ng-checked绑定在一起,AngularJS会输出标准的HTML checked属性,这样默认会把复选框
             勾选:

                 <label>someProperty = {{someProperty}}</label>
                 <input type="checkbox"
                        ng-checked="someProperty"
                        ng-init="someProperty = true"
                        ng-model="someProperty">
                 下面的例子刚好相反:

                 <label>someProperty = {{anotherProperty}}</label>
                 <input type="checkbox"
                        ng-checked="anotherProperty"
                        ng-init="anotherProperty = false"
                        ng-model="anotherProperty">

                 注意,为了展示,这里用ng-model把someProperty和anotherProperty的值绑定到了对应的
             <label>标签里。

                 4. ng-selected

                 ng-selected可以对是否出现option标签的selected属性进行绑定:
                 <label>Select Two Fish:</label>
                 <input type="checkbox"
                        ng-model="isTwoFish"><br/>
                 <select>
                   <option>One Fish</option>
                   <option ng-selected="isTwoFish">Two Fish</option>
                 </select>
                 在线示例:http://jsbin.com/oQazOQE/2/edit。


             9.1.2  类布尔属性

                 ng-href、ng-src等属性虽然不是标准的HTML布尔属性,但是由于行为相似,所以在
             AngularJS源码内部是和布尔属性同等对待的,下面介绍这些属性。

                 ng-href和ng-src都能有效帮助重构和避免错误,因此在改进代码时强烈建议用它们代替原
             来的href和src属性。
                 1. ng-href

                 当使用当前作用域中的属性动态创建URL时,应该用ng-href代替href。
                 这里的潜在问题是当用户点击一个由插值动态生成的链接时,如果插值尚未生效,将会跳转
             到错误的页面(通常是404)。

                 这时,如果使用ng-href,AngularJS会等到插值生效(在例子中是两秒以后)后再执行点击
             链接的行为:

                 <!-- 当 href 包含一个 {{expression}}时总是使用 ng-href -->
                 <a ng-href="{{ myHref }}">I'm feeling lucky, when I load</a>
   73   74   75   76   77   78   79   80   81   82   83