为什么在ng-init中需要变量初始化:AngularJS

时间:2016-06-15 11:22:43

标签: angularjs angularjs-ng-init

我已经开始学习AngularJS了。

在AngularJS中,如果我们必须初始化一个元素,那么我们举一个select:

的例子
<select id="s1" ng-model="vsrc" ng-init="vsrc='a.mp4'">
    <option value="a.mp4">First video</option>
    <option value="b.mp4">Second video</option>
</select>

为什么需要ng-init vsrc =&#39; a.mp4&#39; 的初始化,当我给它的值如 ng-init =&#39; a.mp4&#39; 它无法正常工作我必须像 ng-init =&#34; vsrc一样=&#39; a.mp4&#39;&#34; 即可。在普通的HTML语句中,我们通过提供value='a.mp4'

直接提供默认选项

1 个答案:

答案 0 :(得分:0)

<select id="s1" ng-model="vsrc" ng-init="vsrc='a.mp4'"> /*ng-init used to initialize a **variable** before html render, it is a directive which search left and right value always*/
<option value="a.mp4">First video</option> /*value is here as json object {"value":"a.mp4"}*/
<option value="b.mp4">Second video</option>
</select>

take a look over here about ng-init directive