Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

app.js is:

directive('test', ['name', function ($name) {

                            return {/// DDO
                                template:'<h1>'.$name.'<h1>',
                                link: function () {
                                    console.log($name);
                                }
                            };
                        }]).

While above name a service, which I am injecting into above directive. Above code works fine and data shows up both in console and web page.

BUT

error occurs when I replace template: $name with template: '<h1>'.$name.'</h1>'. The error I get is this:

Uncaught SyntaxError: Unexpected string

So if I can't concatenate string named $name like that with <h1> tags then how do I do it there inside the DDO?

Note: Above given code is definitely not complete code, it's just the part I had problem with. Also service named name was declared/defined/created(or whatever it's called) by using value function.

share|improve this question
up vote 2 down vote accepted

Concatenation in JS is done with the + symbol.

directive('test', ['name', function ($name) {
    return {/// DDO
        template:'<h1>' + $name + '<h1>',
        link: function () {
            console.log($name);
        }
    };
}])
share|improve this answer

To concat string in javascript you have to use +

like this

'<h1>'+$name+'</h1>'

concat string by . is in php not in javascript

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.