【问题标题】:Angular 2 inline template : Variable undefined in strict modeAngular 2内联模板:严格模式下未定义的变量
【发布时间】:2017-04-05 02:55:53
【问题描述】:

Angular 2 应用程序遇到错误,提示“错误:./SampleComponent 类 SampleComponent 中的错误 - 内联模板导致:严格模式下未定义的变量”。 但我只在 IE 10> 中收到此错误。

<input type="text" class="form-control" name="name" formControlName="name" placeholder="Name" [nameFormatter]="selected">

这里传递给属性指令nameFormatter 的“selected”值是一个在组件中初始化的变量。如果我尝试使用一些静态值删除selected 变量,这似乎工作正常。分配它使用 selected 变量会导致此问题。

export class NameComponent implements OnInit {
   public selected : string;

    someFunction(){
         this.selected="some value" //The value changes depending on some conditions
    }
}

与 polyfills 有什么关系吗?我尝试将以下 polyfills 添加到 index.html 中,但没有成功。

  <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.min.js"></script>
  <script src="https://npmcdn.com/angular2/es6/dev/src/testing/shims_for_IE.js"></script>
  <script src="https://unpkg.com/core-js/client/shim.min.js"></script>

有人可以指导我吗?

【问题讨论】:

  • 你尝试过初始化变量吗? public selected : string = "";
  • tanx @Arg0n。我尝试添加这些。但效果似乎相同
  • 您必须发布更多代码。我们能看到这个组件的 html 吗?通常错误Variable undefined in strict mode发生在strict mode(TS默认使用)由于没有使用var声明所以这会产生错误somevar = 5虽然这很好var somevar = 5
  • @AhmedMusallam.Tanx 回复。为什么它可以在除 IE 之外的所有其他浏览器中使用?

标签: javascript angular typescript angular-cli


【解决方案1】:

this 内的somefunction 不引用NameComponent,它处于不同的闭包中。

如果您的目标是从视图 (html) 调用函数,请执行以下操作:

export class NameComponent implements OnInit {
   public selected : string;

   someFunction(){
     this.selected="some value" //The value changes depending on some conditions
   }
}

【讨论】:

  • Typo.my bad.Edited the question.函数是NameComponent。
猜你喜欢
  • 1970-01-01
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-11
  • 2013-07-31
  • 1970-01-01
  • 2022-08-20
相关资源
最近更新 更多