$
interval是Angular对本机Javascript
setInterval的包装。
当
$interval使用时,角意识到由间隔功能所做的任何范围的变化,和双向绑定反映了变化。
当
setInterval使用时,角不会意识到由setInterval函数所做的任何范围的变化。
简而言之,该
$interval函数触发Angular的摘要循环,而
setInterval不会触发。
这个笨拙的人展示了差异。
码:
angular.module('DemoApp', []) .controller('IntervalCtrl', function($scope, $interval) { var updateExampleText = function() { console.log('Changing exampleText'); $scope.exampleText = 'Time: ' + new Date().getSeconds(); }; $scope.useInterval = function() { //Show current seconds value 5 times after every 1000 ms $interval(updateExampleText, 1000, 5); }; $scope.useSetInterval = function() { //$scope.exampleText changes are not reflected in the page //because Angular is not aware of the changes. setInterval(updateExampleText, 1000); }; });


