Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
/angular.jsPublic archive

Dev Guide: When to use $scope.$apply()

Adrian edited this pageDec 20, 2017 ·1 revision

When to use $scope.$apply()

AngularJS provides wrappers for common native JS async behaviors:

  • Events =>ng-click
  • Timeouts =>$timeout
  • jQuery.ajax() =>$http

This is just a traditional async function with a$scope.$apply() called at the end, to tell AngularJS that an asynchronous event just occurred.

$scope.$apply() should occur as close to the async event binding as possible.

DoNOT randomly sprinkle it throughout your code. If you are doing
if (!$scope.$$phase) $scope.$apply() it's because you are not high enough in the call stack.

Whenever possible, use AngularJS services instead of native. If you're creating an AngularJS service (such as for sockets) it should have a$scope.$apply() anywhere it fires a callback or broadcast/emit.

For example,$rootScope.$apply($rootScope.$broadcast('receivedMsg', msg)); is valid

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp