问题描述
前言:我是这个领域的新手。
问题:我需要遍历变量,并在某个时间点停止循环。
尝试过的解决方案未成功:
- 使用相同的变量多次调用ng_init-但这是在全局范围内,只有最后一次分配可见;
- 试图在循环中加入一些“突破”;
- 尝试使用JS
- 尝试使用控制器
代码如下:
{{donePrint=0;\"\"}}
<td ng-repeat=\"hour in whours\">
{{ donePrint }}
<h5 ng-if=\"((itemValue('Weather_OWM_ObservationTime') | date: 'd') != (itemValue('Weather_OWM_DateTime_h' + hour) | date: 'd')) && (donePrint == 0)\">{{ donePrint=1;\"\" }}{{ donePrint }}{{ itemValue('Weather_OWM_DateTime_h' + hour) | date: 'EEE' }}</h5>
</td>
做{{donePrint=0;\"\"}}
的想法来自这里:Set angular scope variable in markup
我要实现的目标是初始化donePrint = 0
,并在第一个积极的ng-if
评估设置donePrint = 1
之后;
上面的代码不会不停止打印 <h5>
标头。取而代之的是0和1交替出现……看起来donePrint
的范围是多少?
我尝试过的另一个版本:
<td ng-app=\"\" ng-repeat=\"hour in whours\">
{{ initFirstPrint() }}
xx-{{ firstprint }}-xx
<h5 ng-if=\"((itemValue('Weather_OWM_ObservationTime') | date: 'd') != (itemValue('Weather_OWM_DateTime_h' + hour) | date: 'd')) && (firstprint == 0)\">{{ doneFirstPrint() }}yy-{{ firstprint }}-yy{{ itemValue('Weather_OWM_DateTime_h' + hour) | date: 'EEE' }}</h5>
</td>
控制器:
(function () {
'use strict';
angular
.module('app.widgets.owm',[])
.controller('owm-forecast-3hx9',['$rootScope','$scope','OHService',function ($rootScope,$scope,OHService) {
$scope.fhours = ['3','6','9','12','15','18','21','24','27'];
$scope.whours = ['6','27'];
$scope.initFirstPrint = function() { $scope.firstprint = 0;}
$scope.doneFirstPrint = function() { $scope.firstprint = 1;}
}
]);
})();
上面的示例工作正常,除了firstprint
始终设置为1,即xx-{{ firstprint }}-xx
和yy-{{ firstprint }}-yy
的打印输出始终为1。
我希望仅在doneFirstPrint
评估为true时对ng-if
进行评估,但事实并非如此。我对AngularJs的了解仍然是错误的...
谢谢您的支持!
解决方法
我自己找到了解决方案-仅将{{ initFirstPrint() }}
放在错误的位置(愚蠢的我...)。但这就是 break 循环退出的解决方案:
HTML代码:
{{ initFirstPrint() }}
<td ng-app=\"\" ng-repeat=\"hour in whours\">
<h5 ng-if=\"((itemValue('Weather_OWM_ObservationTime') | date: 'd') != (itemValue('Weather_OWM_DateTime_h' + hour) | date: 'd')) && (firstprint == 0)\">{{ doneFirstPrint() }}{{ itemValue('Weather_OWM_DateTime_h' + hour) | date: 'EEE' }}</h5>
</td>
控制器代码:
(function () {
'use strict';
angular
.module('app.widgets.owm',[])
.controller('owm-forecast-3hx9',['$rootScope','$scope','OHService',function ($rootScope,$scope,OHService) {
$scope.fhours = ['3','6','9','12','15','18','21','24','27'];
$scope.whours = ['6','27'];
$scope.initFirstPrint = function() { $scope.firstprint = 0;}
$scope.doneFirstPrint = function() { $scope.firstprint = 1;}
}
]);
})();