ngNonBindable
应该忽略当前DOM元素的内容,但在某些情况下它看起来不会。
让我们来看看这个例子:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="">
<div title="{{1 + 2}}">Test 1 - See title</div>
<div ng-non-bindable title="Ignored: {{1 + 2}}">Test 2 - See title</div>
<!-- Uncomment following line and check JS console: -->
<!-- <div ng-non-bindable title="Is it really ignored: {{invalid expression}}">Test 3 - See title</div> -->
</body>
上面的代码按预期工作:
Test 1
,则title属性设置为3
Test 2
,则会忽略title属性,因此设置为Ignored {{1 + 2}}
现在有趣的部分 - 如果您尝试取消注释已注释的代码并运行它,将抛出此异常:
Error: [$parse:syntax] http://errors.angularjs.org/1.2.8/$parse/syntax?p0=test&p1=is%20an%20unexpected%20token&p2=6&p3=test%20test&p4=test
at Error (native)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:6:449
at Ya.throwError (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:155:141)
at Ya.parse (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:154:6)
at $get (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:91:203)
at g (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:73:117)
at S (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:57:109)
at J (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:44:286)
at N (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:42:489)
at N (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:43:145)
我希望Test 3
将标题设置为Is it really ignored: {{invalid expression}}
,这不是一个案例。所以对我来说,看起来只有有效的表达式被忽略,这没有多大意义。这是一个错误 - 或者我错过了什么?你知道怎么绕过这个吗?