Last active
May 10, 2016 17:43
-
-
Save w3cj/3ba748a0241dcf044b0449aabb9228f3 to your computer and use it in GitHub Desktop.
When to call $scope.$apply()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| angular.module('RedditApp', []) | |
| .controller('RedditController', RedditController); | |
| function RedditController($scope){ | |
| // Example of async with jQuery here | |
| // You should really use $http instead which will call $apply for you | |
| $.get('https://www.reddit.com/.json') | |
| .done(function(result){ | |
| console.log(result); | |
| $scope.redditPosts = result.data.children; | |
| //$scope.$apply(); // Have to call this or a digest cycle will not be triggered and the view will not be updated | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html ng-app="RedditApp"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Reddit</title> | |
| <link rel="stylesheet" href="https://bootswatch.com/paper/bootstrap.css" media="screen" title="no title" charset="utf-8"> | |
| </head> | |
| <body ng-controller="RedditController"> | |
| <header class="page-header text-center"> | |
| <h1>Reddit</h1> | |
| </header> | |
| <main class="container"> | |
| <div ng-repeat="post in redditPosts"> | |
| {{post.data.title}} | |
| </div> | |
| </main> | |
| <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.js" charset="utf-8"></script> | |
| <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.js" charset="utf-8"></script> | |
| <script src="app.js" charset="utf-8"></script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment