user4904589
user4904589

Reputation: 565

`Uncaught SyntaxError: Unexpected string` in Angularjs app

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.

Upvotes: 0

Views: 2416

Answers (2)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

To concat string in javascript you have to use +

like this

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

concat string by . is in php not in javascript

Upvotes: 1

Pedro M. Silva
Pedro M. Silva

Reputation: 1298

Concatenation in JS is done with the + symbol.

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

Upvotes: 2

Related Questions