2

Is there a way to pass a value from a Django view to an AngularJS inserted template?

In my view.py I have the code:

count_users = Profile.objects.filter(user_id__gte = 0).count()
context_dict = {'users': count_users}
return render_to_response('dashboard.html', context_dict)

In dashboard.html I am able to insert the user count into the html as follows:

{{ users }}

This works fine but dashboard.html uses AngularJS to insert some more html as follows:

<div ui-view class="fade-in-up">
</div>

Mt problem that the html file inserted by AngularJS does not respond to the:

{{ users }}

Is there a way to pass the value of users through to the AngularJS inserted HTML?

3
  • The users variable has to be attached to the $scope, otherwise it will not appear in such a binding. Commented Sep 20, 2015 at 18:40
  • Can you explain what you mean by attached to the $scope? I don't know what that is. How would I do that? Commented Sep 20, 2015 at 19:44
  • 2
    In angular you have a variable that is called $scope. If you do $scope.someVar = 5 and then {{ someVar }}, it will appear. If you do just var someVar = 5 and then try to bind it the same way - it will not work. So to display the variable you want properly, you must use javascript and attach it to the scope. Commented Sep 20, 2015 at 19:47

2 Answers 2

4

using ng-init you can attach your value into the $scope

<div ui-view class="fade-in-up" ng-init="User='{{user}}' " >

</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that is perfect
Can you share your Django and AngularJS code? This solution is not working for me. Thanks.
0

In your javascript do:

mainModule
    .config(function($interpolateProvider) {
        $interpolateProvider.startSymbol('{$');
        $interpolateProvider.endSymbol('$}');
});

This way, if you want to bind an angular variable in the html, you use:

{$ variable $}

If you want to add a Django variable, you use:

 {{ variable }}

And your code may work if you leave it as it is, and just add this configuration to the angular module.

Comments

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.