Search code examples
angularjsdjangoangularjs-ng-initng-init

Context dictionary from Django template to Angular


I am building a web app and I want to know what is the best and the safest way to pass a variable from a django context dictionary to Angular.

I have access to a variable of the context dictionary from Django template. Should I use ng-init to pass that value to an Angular variable?

It is about a user profile page and I want the user to be able to update his Data. The Data is initialized when calling the view but when I want him to update that Data i want to post using an Angular JS variable.

Thanks in advance


Solution

  • I would add the data as the initial value of each form tag. This answer has a useful "initial" directive to do it in a way that would allow you to use the form even without Angular.

    <input name="profile[name]" value="Joe" ng-model="profile.name" initial>
    

    With that, Angular would take the initial value fro the value attribute (as it should be), so that your form would have an initial value even without Angular running.