Skip to content

Instantly share code, notes, and snippets.

@niquola
Last active September 22, 2023 21:10
Show Gist options
  • Save niquola/5851861 to your computer and use it in GitHub Desktop.
Save niquola/5851861 to your computer and use it in GitHub Desktop.
Progress indication in angularjs, while promises :)
MyController = ($scope, $resource)->
$scope.items = $resource.query()
window.ngApp.directive 'progressLoader', ($filter) ->
restrict: "A"
scope: null
link: (scope, element, attributes) ->
watchCallback = (newValue, oldValue) ->
if newValue?.$resolved == false
element.show()
else
element.hide()
scope.$watch attributes.promise, watchCallback, true
<div class="loader" progress-loader promise="items"> Loading...do more with css :)</div>
<ul>
<li ng-repeat="item in items"> {{item.label}}</li>
</ul>
@smmccrohan
Copy link

For this specific case (waiting on a list that's being used in a repeat), I think it would be cleaner to just use:

<div class="loader" ng-hide="items.length > 0"> Loading... </div>

@niquola
Copy link
Author

niquola commented Jun 24, 2013

thx for simplification,
but i'm looking for generic solution
i don't want to guess about promise result.

This directive can be extended with animation and progress percentage, based on request statistics.

@ThomasBurleson
Copy link

I agree with @smmccrohan. Using promise="items" attribute is a distortion of the concept of promises.
Using ng-hide allows any process to update/modify that data model... this is a better approach AND it eliminates the need for the extra directive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment