简体   繁体   English

AngularJS中ngModel指令的范围是什么?

[英]What is the scope of ngModel directive in AngularJS?

I think that ngModel directive should not create new scope as it needs to make changes in the variables of parent scope. 我认为ngModel指令不应创建新范围,因为它需要更改父范围的变量。
Please correct me if i am wrong . 如果我错了,请纠正我。
And also looking at the source of ngModel directive scope is not defined so it should not create a new scope for directive. 并且还要查看ngModel指令范围的来源是否未定义,因此它不应为指令创建新的范围。

var ngModelDirective = ['$rootScope', function($rootScope) {
  return {
    restrict: 'A',
    require: ['ngModel', '^?form', '^?ngModelOptions'],
    controller: NgModelController,
    // Prelink needs to run before any input directive
    // so that we can set the NgModelOptions in NgModelController
    // before anyone else uses it.
    priority: 1,
    compile: function ngModelCompile(element) {
      // Setup initial state of the control
      element.addClass(PRISTINE_CLASS).addClass(UNTOUCHED_CLASS).addClass(VALID_CLASS);

      return {
        pre: function ngModelPreLink(scope, element, attr, ctrls) {
          var modelCtrl = ctrls[0],
              formCtrl = ctrls[1] || modelCtrl.$$parentForm;

          modelCtrl.$$setOptions(ctrls[2] && ctrls[2].$options);

          // notify others, especially parent forms
          formCtrl.$addControl(modelCtrl);

          attr.$observe('name', function(newValue) {
            if (modelCtrl.$name !== newValue) {
              modelCtrl.$$parentForm.$$renameControl(modelCtrl, newValue);
            }
          });

          scope.$on('$destroy', function() {
            modelCtrl.$$parentForm.$removeControl(modelCtrl);
          });
        },
        post: function ngModelPostLink(scope, element, attr, ctrls) {
          var modelCtrl = ctrls[0];
          if (modelCtrl.$options && modelCtrl.$options.updateOn) {
            element.on(modelCtrl.$options.updateOn, function(ev) {
              modelCtrl.$$debounceViewValueCommit(ev && ev.type);
            });
          }

          element.on('blur', function() {
            if (modelCtrl.$touched) return;

            if ($rootScope.$$phase) {
              scope.$evalAsync(modelCtrl.$setTouched);
            } else {
              scope.$apply(modelCtrl.$setTouched);
            }
          });
        }
      };
    }
  };
}];


Also I don't understand why ngModel directive requires ngModel itself. 另外我也不明白为什么ngModel指令需要ngModel本身。
require: ['ngModel', '^?form', '^?ngModelOptions'] Can't it be ignored and written like require:['ngModel','^?form','^?ngModelOptions']不能像这样被忽略和书写
require: ['^?form', '^?ngModelOptions'] 要求:['^?form','^?ngModelOptions']
If not then please explain why ? 如果没有,请解释原因?

ngModel doesn't create an isolated scope. ngModel不会创建隔离范围。 The reason ngModel is listed in the require array is so that its controller (NgModelController) will be injected into the link function. ngModel在require数组中列出的原因是,其控制器(NgModelController)将被注入到链接函数中。 Notice the ctrls argument that is passed into the ngModelPostLink function. 注意传递给ngModelPostLink函数的ctrls参数。 Because ngModel is listed in the array, ctrls[0] will be an instance of the NgModelController. 由于ngModel在数组中列出,因此ctrls [0]将是NgModelController的实例。 ctrls[1] is the form controller, etc. ctrls [1]是表单控制器,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM