javascript - 创建 AngularJS 指令时 '?ngModel' 的意义是什么?

标签 javascript angularjs angularjs-directive

我正在学习新的 ng-book。关于过滤器的章节包括使用以下代码定义解析器的部分:

angular.module('myApp')
.directive('oneToTen', function() {
    return {
        require: '?ngModel';

我第一次看到 '?ngModel' 语法,Angular API 文档没有提供太多帮助。此语法表示什么?

谢谢!

最佳答案

? 是可选指令,^ 是父指令

http://docs.angularjs.org/api/ng.$compile

(no prefix) - Locate the required controller on the current element. Throw an error if not found.
? - Attempt to locate the required controller or pass null to the link fn if not found.
^ - Locate the required controller by searching the element's parents. Throw an error if not found.
?^ - Attempt to locate the required controller by searching the element's parents or pass null to the link fn if not found.

关于javascript - 创建 AngularJS 指令时 '?ngModel' 的意义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20890001/

相关文章:

javascript - 当指针位于页面右侧时自动重新对齐工具提示

javascript - 在HTML部分使用 "."访问对象并在JavaScript部分使用 "[]"?

javascript - 点击后如何在 AngularJS 中一一循环使用 JSON

javascript - AngularJS 属性指令。如何在 'compile' 上添加另一个属性指令

javascript - AngularJS 中的全局 "App" Controller

javascript - 如何使用 jQuery 隐藏所有接下来的 "fieldset"元素?

javascript - 通过 Ionic 检测耳机

javascript - 使用 HTML 或 Javascript 的内容分隔符

css - 如何从 ionic 框架中的 css 类中删除属性?

javascript - 一旦 Rx.Observable 中发生错误,ValueChanges 就会停止工作