0

I have a question about the custom directives in AngularJS.

I have a list in my scope, a list of objects that I use to create tr lines in a table with ng-repeat. The tr line is a custom directive.

I have something like :

<tr custom-tr ng-repeat='element in list'></tr>

As the list is populated via an HTTP call, the list is populated little by little as I have a foreach for each result in my JSON response :

promise.then(function(json) {
  json.data.foreach(function(response) {
      other_promise.then(funtion(element) {
        $scope.list.push(element);
    })
  })

})

So as the $scope.list gets new elements little by little, my table is supposed to display a new line each time, but actually it waits the end of the foreach before rendering the TR.

When I look at the network tab of the inspector I notice that the HTML template of the tr line is loaded AFTER all the promises.

Network inspector

Is there a way to pre-load the template so I can see the lines in my table before all the promises are done ? Or should I do that differently?

Thanking you in advance,

2
  • Try load directive based on array by adding ng-if as like "<tr ng-if="list.length>0" custom-tr ng-repeat='element in list'></tr>". Commented Dec 13, 2018 at 13:31
  • 1
    npmjs.com/package/gulp-ng-template-cache build your app with the templates built in the app.js Commented Dec 13, 2018 at 13:32

1 Answer 1

1

About the first solution (ng-if), I thought about it some time ago but even if the list is not empty (at least one element), it doesn't mean the html template is loaded as it is loaded at the very end. You can have 35 element, if all the promises are not done it won't work.

I tried the second solution with a similar NPM package and it seems to work now.

Thank you!

Sign up to request clarification or add additional context in comments.

2 Comments

you should have answered in the comments not create an answer ;)
Nice. By adding a prebuild step in which you write in the application's script the templates it is supposed to use, you avoid useless ajax requests and the app is way faster to load.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.