javascript - 在 AngularJS 中,在模板内的过滤器中使用 $

标签 javascript angularjs

我一直在审查 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

来自https://gist.github.com/pkozlowski-opensource/4998969

关于javascript - 在 AngularJS 中,在模板内的过滤器中使用 $,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32534192/

相关文章:

javascript - 向对象原型(prototype)添加函数会导致函数出现在所有 'for X in OBJ' 循环中

javascript - Jquery动态添加行不允许保存数据

javascript - 用空格替换破折号(连字符)

javascript - ReactJS 中的 setState 方法没有更新字段

node.js - Grunt Server 和 WebStorm javascript 调试(和 SpyJS)不工作

angularjs - 表单验证 AngularJs 上的条件样式

javascript - 控件未绑定(bind) angularJS

javascript - 在 $scope 定义的方法中使用工厂?

javascript - Angular-Route 和 ng-view div 隐藏页面

angularjs - AngularJS 和 NodeJs 的套接字问题