星空网 > 软件开发 > Java

Part 15 AngularJS ng init directive

The ng-init directive allows you to evaluate an expression in the current scope.  

In the following example, the ng-init directive initializes employees variable which is then used in the ng-repeat directive to loop thru each employee. In a real world application you should use a controller instead of ng-init to initialize values on a scope. 

<!DOCTYPE html><html ="http://www.w3.org/1999/xhtml"><head>  <title></title>  <script src='/images/loading.gif' data-original="Scripts/angular.min.js"></script></head><body ng-app>  <div ng-init="employees = [          { name: 'Ben', gender: 'Male', city: 'London' },          { name: 'Sara', gender: 'Female', city: 'Chennai' },          { name: 'Mark', gender: 'Male', city: 'Chicago' },          { name: 'Pam', gender: 'Female', city: 'London' },          { name: 'Todd', gender: 'Male', city: 'Chennai' }        ]">    <table>      <thead>        <tr>          <th>Name</th>          <th>Gender</th>          <th>City</th>        </tr>      </thead>      <tbody>        <tr ng-repeat="employee in employees">          <td> {{ employee.name }} </td>          <td> {{ employee.gender}} </td>          <td> {{ employee.city}} </td>        </tr>      </tbody>    </table>  </div></body></html>

ng-init should only be used for aliasing special properties of ng-repeat directive. In the following example, ng-init is used to store the index of the parent element in parentIndex variable.

<!DOCTYPE html><html ="http://www.w3.org/1999/xhtml"><head>  <title></title>  <script src='/images/loading.gif' data-original="Scripts/angular.min.js"></script>  <script src='/images/loading.gif' data-original="Scripts/Script.js"></script></head><body ng-app="myModule">  <div ng-controller="myController">    <ul>      <li ng-repeat="country in countries" ng-init="parentIndex = $index">        {{country.name}}        <ul>          <li ng-repeat="city in country.cities">            {{city.name}} - Parent Index = {{ parentIndex }}, Index = {{ $index }}          </li>        </ul>      </li>    </ul>  </div></body></html>

Script.js  

var app = angular    .module("myModule", [])    .controller("myController", function ($scope) {      var countries = [        {          name: "India",          cities: [            { name: "Hyderabad" },            { name: "Chennai" }          ]        },        {          name: "USA",          cities: [            { name: "Los Angeles" },            { name: "Chicago" },          ]        }      ];       $scope.countries = countries;    });

 

 



原标题:Part 15 AngularJS ng init directive

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

澳洲年检年审:https://www.goluckyvip.com/tag/76272.html
普陀区年审:https://www.goluckyvip.com/tag/76273.html
外省车深圳年审:https://www.goluckyvip.com/tag/76274.html
深圳 代办年审:https://www.goluckyvip.com/tag/76275.html
黄浦年审:https://www.goluckyvip.com/tag/76276.html
宝山区年审:https://www.goluckyvip.com/tag/76277.html
深度解析:美国公司股东人数限制 :https://www.kjdsnews.com/a/1840835.html
深度解析:美国公司股东人数限制 :https://www.xlkjsw.com/news/88201.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流