我一直在审查 bootstrap 的 Angular Directive(指令),here 。特别是 typeahead 指令。
在提供的示例代码中,出现此行:
<input type="text" ng-model="selected" typeahead="state for state in states | filter:$viewValue | limitTo:8" class="form-control">
称我为注意力过滤器:$viewValue。 $viewValue 的目的是什么?那里的美元符号有什么用?
谢谢。
最佳答案
以$
开头的变量是保留的 Angular 变量。请参阅https://docs.angularjs.org/tutorial/step_05在“前缀命名约定”下
As a naming convention, Angular's built-in services, Scope methods and a few other Angular APIs have a $ prefix in front of the name.
The $ prefix is there to namespace Angular-provided services. To prevent collisions it's best to avoid naming your services and models anything that begins with a $.
If you inspect a Scope, you may also notice some properties that begin with $$. These properties are considered private, and should not be accessed or modified.
更具体地说,$viewValue
是用户在输入字段中输入的值。我能找到的所有文档是:
typeahead='sourceExpression'
- source for the typeahead, where sourceExpression can be either an array or a promie resolving to an array. A special variable$viewValue
is available while evaluating this expression
关于javascript - 在 AngularJS 中,在模板内的过滤器中使用 $,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32534192/