GVKun编程网logo

angularjs – 1.3.0 rc0之后ngChange的问题(angularjs constant)

15

对于angularjs–1.3.0rc0之后ngChange的问题感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解angularjsconstant,并且为您提供关于angularJsng-

对于angularjs – 1.3.0 rc0之后ngChange的问题感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解angularjs constant,并且为您提供关于angularJs ng-change 报错、AngularJS ng-change 指令的详解及简单实例、AngularJs ng-change事件/指令、AngularJs ng-change事件/指令的用法小结的宝贵知识。

本文目录一览:

angularjs – 1.3.0 rc0之后ngChange的问题(angularjs constant)

angularjs – 1.3.0 rc0之后ngChange的问题(angularjs constant)

我一直在使用1.3的beta版本,现在转移到1.3.1后,我注意到一个问题,通过检查所有早期版本,我看到它似乎已经在1.3.0 rc1中启动了.

我有这样的代码:

<select ng-model="home.modal.topicId"
        ng-change="ctrl.modalTopicChanged()"
        ng-options="item.id as item.name for item in home.modal.option.topics.data"
        ng-required="true">
        <optionhttps://www.jb51.cc/tag/dis/" target="_blank">display: none;" value="">Select Topic</option>
</select>

在rc1之前,首次显示表单时未触发ng-change.现在它被一个未定义的home.modal.topicId解雇了.这对我来说是一个突破性的变化,但它没有在突破性变化部分提及,我想知道它是否是一个尚未被注意到的错误.

这是生成的堆栈跟踪:

TypeError: Cannot read property 'dataMap' of undefined
    at AdminProblemController.modalTopicChanged (http://127.0.0.1:17315/Content/app/admin/controllers/ProblemController.js:109:114)
    at $parseFunctionCall (http://127.0.0.1:17315/Scripts/angular.js:11387:18)
    at Scope.$get.Scope.$eval (http://127.0.0.1:17315/Scripts/angular.js:13276:28)
    at http://127.0.0.1:17315/Scripts/angular.js:19888:13
    at http://127.0.0.1:17315/Scripts/angular.js:19499:9
    at forEach (http://127.0.0.1:17315/Scripts/angular.js:331:20)
    at $$writeModelToScope (http://127.0.0.1:17315/Scripts/angular.js:19497:5)
    at writetoModelIfNeeded (http://127.0.0.1:17315/Scripts/angular.js:19490:14)
    at http://127.0.0.1:17315/Scripts/angular.js:19484:9
    at validationDone (http://127.0.0.1:17315/Scripts/angular.js:19420:9)

我在这里注意到的是一个新函数:writetoModelIfNeeded

当我查看更改日志差异时,在检查所有更改和行号时,我找不到提及此功能的任何内容.

我想就此提出一些建议.首先是可以找到导致添加writetoModelIfNeeded的更改,其次是选择框的正确功能.我认为整个想法是只有在定义模型值时才会触发ng-change.

这里参考的是新代码的区域,似乎已经添加了1.3.0 rc.1

**
   * @ngdoc method
   * @name ngModel.NgModelController#$commitViewValue
   *
   * @description
   * Commit a pending update to the `$modelValue`.
   *
   * Updates may be pending by a debounced event or because the input is waiting for a some future
   * event defined in `ng-model-options`. this method is rarely needed as `NgModelController`
   * usually handles calling this in response to input events.
   */
  this.$commitViewValue = function() {
    var viewValue = ctrl.$viewValue;

    $timeout.cancel(pendingDebounce);

    // If the view value has not changed then we should just exit,except in the case where there is
    // a native validator on the element. In this case the validation state may have changed even though
    // the viewValue has stayed empty.
    if (ctrl.$$lastCommittedViewValue === viewValue && (viewValue !== '' || !ctrl.$$hasNativeValidators)) {
      return;
    }
    ctrl.$$lastCommittedViewValue = viewValue;

    // change to dirty
    if (ctrl.$pristine) {
      ctrl.$dirty = true;
      ctrl.$pristine = false;
      $animate.removeClass($element,PRISTINE_CLASS);
      $animate.addClass($element,DIRTY_CLASS);
      parentForm.$setDirty();
    }
    this.$$parseAndValidate();
  };

  this.$$parseAndValidate = function() {
    var parserValid = true,viewValue = ctrl.$$lastCommittedViewValue,modelValue = viewValue;
    for(var i = 0; i < ctrl.$parsers.length; i++) {
      modelValue = ctrl.$parsers[i](modelValue);
      if (isUndefined(modelValue)) {
        parserValid = false;
        break;
      }
    }
    if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
      // ctrl.$modelValue has not been touched yet...
      ctrl.$modelValue = ngModelGet();
    }
    var prevModelValue = ctrl.$modelValue;
    var allowInvalid = ctrl.$options && ctrl.$options.allowInvalid;
    if (allowInvalid) {
      ctrl.$modelValue = modelValue;
      writetoModelIfNeeded();
    }
    ctrl.$$runValidators(parserValid,modelValue,viewValue,function() {
      if (!allowInvalid) {
        ctrl.$modelValue = ctrl.$valid ? modelValue : undefined;
        writetoModelIfNeeded();
      }
    });

    function writetoModelIfNeeded() {
      if (ctrl.$modelValue !== prevModelValue) {
        ctrl.$$writeModelToScope();
      }
    }
  };

  this.$$writeModelToScope = function() {
    ngModelSet(ctrl.$modelValue);
    forEach(ctrl.$viewchangelisteners,function(listener) {
      try {
        listener();
      } catch(e) {
        $exceptionHandler(e);
      }
    });
  };
通过这样做,我能够重现你的问题.没有看到你的控制器虽然不确定是否相同:
this.modal = {
      topicId:null,option:{
        topics:{
          data:[{id:1,name:'item1'},{id:2,name:'item2'}]
        }
      }
    };

这里发生的是angular表示null是一个无效值,因此默认情况下将其设置为undefined.您可以通过将其设置为“未定义”或将其添加到您的html来解决此问题:

ng-model-options="{allowInvalid:true}"

还测试了Josep plunker并将该值更改为null也导致ngChange触发

angularJs ng-change 报错

angularJs ng-change 报错

无论在 input 还是 select 上加 ng-change 都会报错

代码如下:

    <input ng-change="mychange()"  type="text"/>


报错代码:

[$compile:ctreq] Controller ''ngModel'', required by directive ''ngChange'', can''t be found!

AngularJS ng-change 指令的详解及简单实例

AngularJS ng-change 指令的详解及简单实例

AngularJS ng-change 指令

AngularJS 实例

当输入框的值改变时执行函数:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl">
 <p>在输入框中输入一些信息:</p>
 <input type="text" ng-change="myFunc()" ng-model="myValue" />
 <p>输入框已经修改了 {{count}} 次。</p>
</div>

<script>
 angular.module(''myApp'', [])
  .controller(''myCtrl'', [''$scope'', function($scope) {
   $scope.count = 0;
   $scope.myFunc = function() {
    $scope.count++;
   };
  }]);
</script>
</body>
</html>

运行结果:

 在输入框中输入一些信息:

输入框已经修改了 0 次。

定义和用法

ng-change 指令用于告诉 AngularJS 在 HTML 元素值改变时需要执行的操作。

ng-change 指令需要搭配 ng-model 指令使用。

AngularJS ng-change 指令指令不会覆盖原生的 onchange 事件, 如果触发该事件,ng-change 表达式与原生的 onchange 事件都会执行。

ng-change 事件在值的每次改变时触发,它不需要等待一个完成的修改过程,或等待失去焦点的动作。

ng-change 事件只针对输入框值的真实修改,而不是通过 JavaScript 来修改。

语法

<element ng-change="expression"></element>

<input>, <select>, 和  <textarea> 元素支持。

参数值

描述
expression 元素值改变时执行表达式。

以上就是对AngularJS ng-change 指令的知识整理,后续继续补充。

您可能感兴趣的文章:
  • 详解Angular系列之变化检测(Change Detection)
  • 解决angularJS中input标签的ng-change事件无效问题
  • angularjs实现对表单输入改变的监控(ng-change和watch两种方式)
  • AngularJs ng-change事件/指令的用法小结
  • Angular 的 Change Detection机制实现详解

AngularJs ng-change事件/指令

AngularJs ng-change事件/指令

定义和用法

ng-change指令用于告诉 AngularJS 在 HTML 元素值改变时需要执行的操作。

ng-change指令需要搭配ng-model指令使用。

AngularJSng-change指令指令不会覆盖原生的 onchange 事件,如果触发该事件,ng-change表达式与原生的 onchange 事件都会执行。

ng-change事件在值的每次改变时触发,它不需要等待一个完成的修改过程,或等待失去焦点的动作。

ng-change事件只针对输入框值的真实修改,而不是通过 JavaScript 来修改。


语法

< element ng-change= "expression" > /element >

<input>,<select>,和 <textarea> 元素支持。

<radio>,<checkBox>


参数值

描述
expression 元素值改变时执行表达式。

实例说明,radio和checkBox

注:checkBox ng-model总是是true或false,而不是value,其他的ng-model默认都是value 的值

HTML

<h3>Radio 控件测试</h3>
<p><label>
        <input type="radio" value="男" name="sex" ng-model="value1" ng-change="radioChecked()" />
        男
    </label>
    <label>
        <input type="radio" value="女" name="sex" ng-model="value1" ng-change="radioChecked()" />
        女
    </label></p>
<h3>checked 控件测试</h3>
<p><divhttps://www.jb51.cc/tag/Box/" target="_blank">Box">
        <label>
            <input name="agree" type="checkBox" value="同意" ng-model="value2" ng-change="checkBoxClick()" />
            同意协议
        </label>
    </div>
    <divhttps://www.jb51.cc/tag/Box/" target="_blank">Box">
        <label>
            <input name="agree" type="checkBox" value="同意2" ng-model="value2" ng-change="checkBoxClick()" />
            同意协议2
        </label>
    </div></p>
JS:
var app = angular.module('myApp',[]);
app.controller('validateCtrl',function ($scope) {
    //randio ng-change事件和原始onchange相同
    //radio ng-model 的值是value
    $scope.radioChecked = function () {
        console.info($scope.value1);
    }
    //checkBox ng-change事件和原始onchange相同
    //checkBox ng-model总是是true或false
    $scope.checkBoxClick = function () {
        console.info($scope.value2);
    }
});

实例说明,text,select

HTML

<form>
    <div>
        <label>姓名:</label>
        <input type="text"ng-model="name" ng-change="txtChange();" />
    </div>
    <div>
        <label>选择年级:</label>
        <selectng-change="selectChange();" ng-model="grade">
            <option value="1">一年级</option>
            <option value="2">二年级</option>
        </select>
    </div>
</form>
JS
var app = angular.module('myApp',function ($scope) {
    //textBox 的ng-change事件和原始ng-change不相同,而是和$scope.$watch()监听相同
    //textBox 的ng-model为当前输入框的内容,为value值
    $scope.txtChange = function () {
        console.info($scope.name);
    }
    //select 的ng-change事件和原始ng-change相同
    //ng-model 的默认情况下ng-model的结果为value值
    $scope.selectChange = function () {
        console.info($scope.grade);
    }
});

AngularJs ng-change事件/指令的用法小结

AngularJs ng-change事件/指令的用法小结

本文介绍了AngularJs ng-change事件/指令的小结,分享给大家,也给自己留个笔记

定义和用法

ng-change 指令用于告诉 AngularJS 在 HTML 元素值改变时需要执行的操作。

ng-change 指令需要搭配 ng-model 指令使用。

AngularJS ng-change 指令指令不会覆盖原生的 onchange 事件, 如果触发该事件,ng-change 表达式与原生的 onchange 事件都会执行。

ng-change 事件在值的每次改变时触发,它不需要等待一个完成的修改过程,或等待失去焦点的动作。

ng-change 事件只针对输入框值的真实修改,而不是通过 JavaScript 来修改。

语法

<element ng-change="expression"></element>

  1. <input>, <select>, 和  <textarea> 元素支持。
  2. <radio>,<checkbox>

参数值

描述
expression 元素值改变时执行表达式。

实例说明:当输入框的值改变时执行函数:

<body ng-app="myApp">

<div ng-controller="myCtrl">
 <input type="text" ng-change="myFunc()" ng-model="myValue" />
 <p>The input field has changed {{count}} times.</p>
</div>

<script>
angular.module(''myApp'', [])
.controller(''myCtrl'', [''$scope'', function($scope) {
 $scope.count = 0;
 $scope.myFunc = function() {
  $scope.count++;
 };
}]);
</script>

</body>

实例说明,radio和checkbox

注:checkbox ng-model总是是true或false,而不是value,其他的ng-model默认都是value 的值

HTML

<h3>Radio 控件测试</h3> 
<p><label> 
  <input type="radio" value="男" name="sex" ng-model="value1" ng-change="radioChecked()" /> 
  男 
 </label> 
 <label> 
  <input type="radio" value="女" name="sex" ng-model="value1" ng-change="radioChecked()" /> 
  女 
 </label></p> 
<h3>checked 控件测试</h3> 
<p><div> 
  <label> 
   <input name="agree" type="checkbox" value="同意" ng-model="value2" ng-change="checkboxClick()" /> 
   同意协议 
  </label> 
 </div> 
 <div> 
  <label> 
   <input name="agree" type="checkbox" value="同意2" ng-model="value2" ng-change="checkboxClick()" /> 
   同意协议2 
  </label> 
 </div></p> 

JS:

var app = angular.module(''myApp'', []); 
app.controller(''validateCtrl'', function ($scope) { 
 //randio ng-change事件和原始onchange相同 
 //radio ng-model 的值是value 
 $scope.radioChecked = function () { 
  console.info($scope.value1); 
 } 
 //checkbox ng-change事件和原始onchange相同 
 //checkbox ng-model总是是true或false 
 $scope.checkboxClick = function () { 
  console.info($scope.value2); 
 } 
}); 

实例说明,text,select

HTML

<form> 
 <div> 
  <label>姓名:</label> 
  <input type="text"ng-model="name" ng-change="txtChange();" /> 
 </div> 
 <div> 
  <label>选择年级:</label> 
  <selectng-change="selectChange();" ng-model="grade"> 
   <option value="1">一年级</option> 
   <option value="2">二年级</option> 
  </select> 
 </div> 
</form> 

JS

var app = angular.module(''myApp'', []); 
app.controller(''validateCtrl'', function ($scope) { 
 //textbox 的ng-change事件和原始ng-change不相同,而是和$scope.$watch()监听相同 
 //textbox 的ng-model为当前输入框的内容,为value值 
 $scope.txtChange = function () { 
  console.info($scope.name); 
 } 
 //select 的ng-change事件和原始ng-change相同 
 //ng-model 的默认情况下ng-model的结果为value值 
 $scope.selectChange = function () { 
  console.info($scope.grade); 
 } 
}); 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:
  • 详解Angular系列之变化检测(Change Detection)
  • 解决angularJS中input标签的ng-change事件无效问题
  • angularjs实现对表单输入改变的监控(ng-change和watch两种方式)
  • AngularJS ng-change 指令的详解及简单实例
  • Angular 的 Change Detection机制实现详解

关于angularjs – 1.3.0 rc0之后ngChange的问题angularjs constant的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于angularJs ng-change 报错、AngularJS ng-change 指令的详解及简单实例、AngularJs ng-change事件/指令、AngularJs ng-change事件/指令的用法小结等相关内容,可以在本站寻找。

本文标签: