【问题标题】:TypeError: Cannot read property 'bind' of undefined at Controller.NgModelControllerTypeError:无法读取 Controller.NgModelController 处未定义的属性“绑定”
【发布时间】:2020-06-09 22:38:41
【问题描述】:

我正在尝试将我的项目从 angularjs 版本 1.2 更新到 1.7.9。但是我直到 1.5 才发现任何问题。但从 1.6 开始,我收到以下错误:

TypeError: Cannot read property 'bind' of undefined at Controller.NgModelController (VM2951 angular.js:29413)

@angular.js:29413 => this.$$updateEventHandler = this.$$updateEventHandler.bind(this);

package.json

"dependencies": {
  "angular": "1.7.0",
  "angular-animate": "1.7.0",
  "angular-sanitize": "1.7.0",
  "angular-ui-router": "0.3.2",
  "angular-form-lib": "2.2.1",
  ...
}

index.js

var moduleName = module.exports = 'test';

angular
  .module(moduleName, [
    require('angular-ui-router'),
    require('../api/')
  ])
  .config(['$stateProvider', function ($stateProvider) {
    $stateProvider.state('test', {
      url: '/test',
      template: require('./xx.html'),
      controller: 'TestController as vm'
    });
  }])
  .controller('TestController', require('./TestCtrl'))

TestCtrl.js

module.exports = ['$scope','$rootScope',
  function ('$scope','$rootScope') {
      var vm = this;
          vm.toggle = false;

      ....
}];

test.html

<input type="radio" name="test_toggle" id="test-toggle-1" 
                 ng-model="vm.toggle" ng-value="true"
                 ng-change="vm.clickHandler('test')">

此问题主要出现在表单输入字段中,为绑定提供了 ng-model。

感谢您的帮助。

【问题讨论】:

  • 你能把使用ng-model的代码贴出来
  • @SuryapratapSingh 我已经添加了 html 代码。请检查

标签: javascript angularjs angularjs-1.7


【解决方案1】:

我发现问题出在第三方 Angular 表单库之一,它覆盖了 ngmodel 行为。

$injector.invoke(controller, Object.setPrototypeOf(this, controller.prototype), {
   '$attrs': attrs,
   '$scope': scope,
   '$element': element
});

添加此块后,绑定问题消失了。

【讨论】:

    猜你喜欢
    • 2022-06-16
    • 2021-11-07
    • 1970-01-01
    • 2020-11-29
    • 2021-10-08
    • 2017-09-16
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多