ng-init使用ng-repeat不起作用

时间:2015-07-01 13:19:03

标签: javascript html angularjs

我试图在div中显示一些数字,如下所示

 <div ng-init="range=[1,10,3,4,5]" ng-repeat="n in range" >
  {{n}}
 </div> 

未在range

中显示数字

4 个答案:

答案 0 :(得分:3)

在ng-repeat之前初始化

<div ng-init="range=[1,10,3,4,5]" >
<div ng-repeat="n in range" >
  {{n}}
 </div> 
</div>

小提琴解决方案:https://jsfiddle.net/ggbnanoL/

答案 1 :(得分:1)

ng-repeat

之前初始化值
<div ng-init="range=[1,10,3,4,5]"></div>
<div ng-repeat="n in range" >
    {{n}}
</div>

Plunker

答案 2 :(得分:0)

ng-init只能用于别名:https://docs.angularjs.org/api/ng/directive/ngInit

您是否可以初始化控制器中的范围值?

答案 3 :(得分:0)

试试这个

<!DOCTYPE html>
<html ng-app>
<head>
    <title>AngularJS</title>
    <script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>

</head>
<body>
    <ANY ng-init="range=[1,10,3,4,5]">
    </ANY>
    <div ng-repeat="n in range">
    {{n}}
    </div>
</body>
</html>