2

In my application, I'm using NodeJS, Express in the backend and Angular in the frontend. I'm also using Jade template engine.

jade obtains a variable called "adv" by this code.

      res.render('page',{adv:result[0]})

In controller.js (for angular)

        $scope.content = [];

I would like to do something like

        form(ng-init="content=#{adv}") 

            h5 {{"content" + content}}

i.e. assign that jade variable to the scope. It is not working. I could use http.get and get the content directly to angular scope, but just wondering if it is possible to do this

Thanks

9
  • why don't you say $scope.adv = result[0]? Commented Jul 17, 2014 at 5:54
  • Do you get any errors? What does the rendered HTML look like? Commented Jul 17, 2014 at 5:57
  • @Mritunjay - using res.render('page',{$scope.content:result[0]}) gives "SyntaxError: Unexpected token ." Commented Jul 17, 2014 at 6:05
  • @ivarni - Error: [$parse:syntax] errors.angularjs.org/1.2.18/$parse/… only "content" string is displayed Commented Jul 17, 2014 at 6:08
  • The error hints that form(ng-init="content=#{adv}") ends up as <form ng-init="content=[object]"> so it looks like there are two problems at play here. First, jade seems to render result[0] as a JSON object and not a string (maybe try to stringify it?) and second I am pretty sure you need to use single-quotes with ng-init if you want to assign a constant and not a variable. Commented Jul 17, 2014 at 6:13

1 Answer 1

3

This worked.

    - var str_adv = JSON.stringify(adv)

    form(ng-init="content = #{str_adv}") 

or even (where getContent is a function defined in controller doing the same thing)

    form(ng-init="getContent(#{str_adv})") 

Both these also parsed the string and stored the object in 'content'

But directly using

    form(ng-init="content = JSON.stringify(#{adv})")

gives the same error

It seems assigning objects in ng-init is not possible.

Thanks @ivarni for the hint about stringify

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

2 Comments

This doesn't work for me :( My code is ng-init="balance = #{balance}". What am I doing wrong?
You could also use it like this: form(ng-init="content = #{JSON.stringify(adv)}")

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.