AngularJS模板不能使用包含连字符的JSON

Bal*_*ala 12 angularjs

AngularJS模板不能使用密钥中包含连字符的JSON.

例如

我的Json看起来像

{
   ...
   link: {
       xx-test:{
            href: '/test/xx'
         }
}
Run Code Online (Sandbox Code Playgroud)

现在,在我的angularjs模板中,如果我引用href它不起作用

<a ng-href="/app/edit?item={{item.link.xx-test.href}}"></a>
Run Code Online (Sandbox Code Playgroud)

它无法解析值href呈现为/ app/edit?item =

它试过了

<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>
Run Code Online (Sandbox Code Playgroud)

rGi*_*Gil 22

对象键需要引用:

$scope.bar = {'xx-test':'foo'};
Run Code Online (Sandbox Code Playgroud)

角度表达式中应使用括号表示法.

<p>{{bar['xx-test']}}</p>
Run Code Online (Sandbox Code Playgroud)

您可以选择转义\-角度表达式中的连字符.