{"id":3502,"date":"2015-02-06T11:43:19","date_gmt":"2015-02-06T03:43:19","guid":{"rendered":"https:\/\/sdeno.com\/?p=3502"},"modified":"2015-02-06T11:57:03","modified_gmt":"2015-02-06T03:57:03","slug":"apply%e8%a7%a3%e9%87%8a%e8%af%b4%e6%98%8e","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=3502","title":{"rendered":"$apply()\u89e3\u91ca\u8bf4\u660e"},"content":{"rendered":"<p>$apply()\u662f\u7528\u6765\u544a\u8bc9\u5e76\u63d0\u9192angular\uff0c\u5728model\u4e0a\u5df2\u7ecf\u6709\u66f4\u65b0\u4e86\u4e5f\u8bf7\u53ca\u65f6\u66f4\u65b0view\u3002<br \/>\n\u90a3\u4e48\u6211\u4eec\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528$apply()\uff1f<br \/>\n\u4f8b\u5982\uff1a<\/p>\n<pre>angular.module('myApp',[]).controller('MessageController', function($scope) { \r\n \r\n  $scope.getMessage = function() { \r\n     setTimeout(function() { \r\n      $scope.message = 'Fetched after 3 seconds'; \r\n      console.log('message:'+$scope.message); \r\n     }, 2000); \r\n  } \r\n  $scope.getMessage(); \r\n\r\n});<\/pre>\n<p>\u6211\u4eec\u4f7f\u7528\u7684\u662fsetTimeout\u65b9\u6cd5\u66f4\u65b0$scope.message\u7684\u503c\uff0c\u540c\u65f6\u4e5f\u5e0c\u671b\u66f4\u65b0\u7684\u503c\u5728view\u4e5f\u53ca\u65f6\u5237\u65b0\uff0c\u4f46\u662f\u7528\u8fd9\u79cd\u65b9\u6cd5view\u662f\u4e0d\u80fd\u53ca\u65f6\u66f4\u65b0\u7684\u3002\u56e0\u4e3aangular\u5e76\u4e0d\u77e5\u9053$scope.message\u6709\u66f4\u65b0\u3002<\/p>\n<p>\u8fd9\u65f6\u5019\u6211\u4eec\u5c31\u4f7f\u7528$apply()\u4e86\uff0c\u5c06\u66f4\u6539\u7684\u6570\u503c\u5199\u5728$apply()\u91cc\u9762\uff0c\u8fd9\u6837\u5c31\u80fd\u8ba9angular\u77e5\u9053$scope.message\u6709\u53d8\u5316\u4e5f\u8bf7\u53ca\u65f6\u66f4\u65b0view\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>angular.module('myApp',[]).controller('MessageController', function($scope) { \r\n \r\n   $scope.getMessage = function() { \r\n      setTimeout(function() { \r\n        $scope.$apply(function() { \r\n          $scope.message = 'Fetched after 3 seconds'; \r\n          console.log('message:' + $scope.message); \r\n         }); \r\n      }, 2000); \r\n   } \r\n \r\n  $scope.getMessage(); \r\n \r\n });<\/pre>\n<p>\u5982\u679c\u662f\u9700\u8981setTimeout()\u529f\u80fd\u7684\u8bdd\uff0c\u5176\u5b9e\u63a8\u8350\u4f7f\u7528$timeout\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u81ea\u52a8\u8c03\u7528$apply()\u6765\u63d0\u9192angular\u544a\u8bc9\u6709model\u53d8\u5316\u4e86\u3002<\/p>\n<p>\u603b\u7ed3\uff1a\u9700\u8981\u8bb0\u4f4f\u7684\u6700\u91cd\u8981\u7684\u662fAngularJS\u662f\u5426\u80fd\u68c0\u6d4b\u5230\u4f60\u5bf9\u4e8emodel\u7684\u4fee\u6539\u3002\u5982\u679c\u5b83\u4e0d\u80fd\u68c0\u6d4b\u5230\uff0c\u90a3\u4e48\u4f60\u5c31\u9700\u8981\u624b\u52a8\u5730\u8c03\u7528$apply()\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>$apply()\u662f\u7528\u6765\u544a\u8bc9\u5e76\u63d0\u9192angular\uff0c\u5728model\u4e0a\u5df2\u7ecf\u6709\u66f4\u65b0\u4e86\u4e5f\u8bf7\u53ca\u65f6\u66f4\u65b0view\u3002 \u90a3\u4e48\u6211\u4eec\u4ec0\u4e48 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-3502","post","type-post","status-publish","format-standard","hentry","category-angularjs"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/3502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3502"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/3502\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}