div标签中的数组变量在angularJS中的所有其他div标签中复制

时间:2016-11-01 19:25:00

标签: angularjs angularjs-ng-init ng-init

第一个#div1中的数组ar数组在所有div中重复。 所有其他div都具有公共数组变量名ar。我试着用不同的名字。

<body ng-app="">  
    <div id="div1" ng-init="ar=['java','j2ee','c','c++','spring','hibernate']">
        <ul>
            <li ng-repeat="item1 in ar">
                {{item1}}
            </li>
        </ul>
    </div>
    <div id="div2" ng-init="ar=['books','pens','eraser''sharpner','scale','scale']">
        <table border="1">
            <tr ng-repeat="item2 in ar">
                <td>{{item2}}</td>
            </tr>
        </table>
    </div> <br/>
    <div id="div3" ng-init="ar=['html','css','angularjs''PHP','BOOTSTRAP','NODEJS']">
        <table border="1">
            <tr>
                <th>sl.no.</th>
                <th>courses</th>
            </tr>
            <tr ng-repeat="item3 in ar">
                <td>{{$index}}</td>
                <td>{{item3}}</td>
            </tr>
        </table>
    </div>
    <div id="div4" ng-repeat="item4 in ar">
        {{item4}}
    </div>
</body>

<script src="angular.js"></script>

2 个答案:

答案 0 :(得分:0)

如果您对静态值感兴趣,可以跳过完全保留像ar这样的数组变量并直接传递数组。

<li ng-repeat="item1 in ['java','j2ee','c','c++','spring','hibernate']">
    {{item1}}
</li>

<tr ng-repeat="item2 in ['books','pens','eraser''sharpner','scale','scale'] ">
    <td>{{item2}}</td>
</tr>

答案 1 :(得分:0)

您忘记包含逗号

<div id="div2" ng-init="ar=['books','pens','eraser''sharpner','scale','scale']">

<div id="div3" ng-init="ar=['html','css','angularjs''PHP','BOOTSTRAP','NODEJS']">

另一件事,每次初始化时使用相同的变量名称,那些引用的元素使用新值。

您可以尝试使用名称不同的变量定义每个列表,查找此

<body ng-app="">
   <div id="div1" ng-init="ar=['java','j2ee','c','c++','spring','hibernate']">
      <ul>
         <li ng-repeat="item1 in ar">
            {{item1}}
         </li>
      </ul>
   </div>
   <div id="div2" ng-init="br=['books','pens','eraser','sharpner','scale','scale']">
      <table border="1">
         <tr ng-repeat="item2 in br">
            <td>{{item2}}</td>
         </tr>
      </table>
   </div>
   <br/>
   <div id="div3" ng-init="cr=['html','css','angularjs','PHP','BOOTSTRAP','NODEJS']">
      <table border="1">
         <tr>
            <th>sl.no.</th>
            <th>courses</th>
         </tr>
         <tr ng-repeat="item3 in cr">
            <td>{{$index}}</td>
            <td >{{item3}}</td>
         </tr>
      </table>
   </div>
   <div id="div4" ng-repeat="item4 in ar" >
      {{item4}}
   </div>
</body>

这里有jsfiddle