Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS templates can't use JSON that contains hyphen

Tags:

angularjs

AngularJS templates can't use JSON that contains hyphen in the key.

e.g.

My Json looks like

{
   ...
   link: {
       xx-test:{
            href: '/test/xx'
         }
}

Now, in my angularjs template if I refer the href it is not working

<a ng-href="/app/edit?item={{item.link.xx-test.href}}"></a>

It is unable to resolve the value href is rendered as /app/edit?item=

It tried

<a ng-href="/app/edit?item={{'item.link.xx-test.href'}}"></a>
<a ng-href="/app/edit?item={{item.link.xx\-test.href}}"></a>
<a ng-href="/app/edit?item={{item.['link.xx-test'].href}}"></a>
like image 920
Bala Avatar asked Jul 10 '13 22:07

Bala


People also ask

Is hyphen allowed in JSON?

You cannot declare variable with the character '-'. The json file might be a typo so I would probably just modify the json file property to read uploadTime and go from there. I dare say that most json files you will be getting wont have hyphen in the names so you shouldn't come across it again.

What is the Language of the Angular template?

In Angular, a template is a chunk of HTML.


1 Answers

The object key needs to be quoted with:

$scope.bar = {'xx-test':'foo'};

Bracket notation should be used in the angular expression.

<p>{{bar['xx-test']}}</p>

You can optionally escape the hyphen \- in the angular expression.

like image 70
rGil Avatar answered Nov 06 '22 02:11

rGil