ControllerAs模式:var vm = this-自从更新到AngularJS 1.7以来,范围未定义 [英] ControllerAs pattern: var vm = this - scope not defined since update to AngularJS 1.7

查看:171
本文介绍了ControllerAs模式:var vm = this-自从更新到AngularJS 1.7以来,范围未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从AngularJS 1.7(从1.6开始)更新以来,似乎有破灭的机会,或者至少是我们做错了,现在导致了未定义的问题.

Since update to AngularJS 1.7 (from 1.6) there seem to a breaking chance or at least something we did wrong and now results in an undefined issue.

我试图查阅AngularJS更新日志,进行了很多搜索和尝试,但是找不到解决方案.

I tried to consult AngularJS changelog, searched and tried a lot but cannot find the solution.

定向呼叫-已验证myAllocation不为null并具有所有必需的属性

Directive Call - it's verified that myAllocation is not null and has all required properties

<ac-allocation ac-allocation="myAllocation"></ac-allocation>

指令代码

function acAllocation(SomeService) {

return {
  restrict: 'E',
  replace: true,
  scope: {
    allocation: '=acAllocation'
  },
  controller: acAllocationController,
  controllerAs: 'vm',
  bindToController: true,
  templateUrl: 'components/some.html'
};

function acAllocationController() {
  var vm = this;

  if (vm.allocation) {

  // this fails as vm.allocation is not defined anymore, but it used to be
  }
}
}

推荐答案

找到了它:-(

自从我删除了preAssignBindingsEnabled标志(因为它已弃用),现在在AngularJs 1.7中已删除将AngularJs 1.5更改为1.6 现在正在打我们:

Since I removed the preAssignBindingsEnabled flag as it was deprecated and now removed in AngularJs 1.7 this breaking change from AngularJs 1.5 to 1.6 is now hitting us:

所以在我们的情况下,我现在必须使用:

So in our case I have to use now:

function acAllocationController() {

  this.$onInit = function() {
    var vm = this;
    if (vm.allocation) {
    //...

确保绑定可用.

现在是我的问题(几个月前应该是我的问题,但我没有看到):

My issue is now (and it should have been my issue already months back, but I didn't see it):

如何确定所有其他可能影响我的潜在指令? :-(

这篇关于ControllerAs模式:var vm = this-自从更新到AngularJS 1.7以来,范围未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆