最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the significance of '?ngModel' when creating an AngularJS directive? - Stack Overflow

programmeradmin5浏览0评论

I'm working my way through the new ng-book. The chapter on filters includes a section on defining parsers with the following code:

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

The first time I've seen the '?ngModel' syntax, and the Angular API docs don't provide much help. What does this syntax signify?

Thanks!

I'm working my way through the new ng-book. The chapter on filters includes a section on defining parsers with the following code:

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

The first time I've seen the '?ngModel' syntax, and the Angular API docs don't provide much help. What does this syntax signify?

Thanks!

Share Improve this question asked Jan 2, 2014 at 19:08 Brian PiercyBrian Piercy 6491 gold badge7 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

? is optional directive and ^ is parent directive

http://docs.angularjs/api/ng.$pile

(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.

The Required can be explained as:

[?][^][directiveName].

It is used to specify which directive controller should be used("inherited from"). So for instance a directive <column-item> needs to find the parent controller <crtl-grid>. There are a couple of symbols that can be used along with this attribute and they can also be bined:

^ = it indicates angular to seek up the DOM to find the directive.

? = it indicates angular that the directive is optional and angular will not throw an exception if not found.

So ?ngModel is saying that the ngModel needs be declared along with this directive.

发布评论

评论列表(0)

  1. 暂无评论