【问题标题】:Error: Cannot assign to a reference or variable! Angular 4 [duplicate]错误:无法分配给引用或变量! Angular 4 [重复]
【发布时间】:2018-06-09 20:01:11
【问题描述】:

在添加一些新的 html 代码后,我在 angular 4 中遇到错误,之前表单工作正常。我尝试评论新代码,但它仍然无法正常工作 - 该怎么办 - 我研究了这个错误并尝试解决,但没有任何帮助 - 我也没有得到问题的确切位置

我的代码:

<form role="form" #adminForm="ngForm" (ngSubmit)="logAdmin(adminForm.form)">
          <div class="form-group">
            <input class="form-control" [(ngModel)]="adminUsername" #adminUsername="ngModel" placeholder="Username" type="text" name="adminUsername"
              required>
            <div *ngIf="adminUsername.touched && adminUsername.errors">
              <div class="alert alert-danger" *ngIf="adminUsername.errors.required">Username is required</div>
            </div>
          </div>
          <div class="form-group">
            <input class="form-control" [(ngModel)]="passwordText" #adminPassword="ngModel" id="adminPassword" name="adminPassword" placeholder="Password"
              type="password" required>
          </div>
          <div class="alert alert-danger" *ngIf="adminPassword.touched && adminPassword.errors">Password is required</div>
          <div class="col-md-6" style="text-align: right">
            <button type="submit" class="btn-yellow" [disabled]="!adminForm.valid">Log In</button>
          </div>
        </form>

我得到的错误:

Error: Cannot assign to a reference or variable!
    at _AstToIrVisitor.visitPropertyWrite (webpack-internal:///../../../compiler/esm5/compiler.js:26550:23)
    at PropertyWrite.visit (webpack-internal:///../../../compiler/esm5/compiler.js:4895:24)
    at convertActionBinding (webpack-internal:///../../../compiler/esm5/compiler.js:26000:45)
    at eval (webpack-internal:///../../../compiler/esm5/compiler.js:28519:22)
    at Array.forEach (<anonymous>)
    at ViewBuilder._createElementHandleEventFn (webpack-internal:///../../../compiler/esm5/compiler.js:28515:18)
    at nodes.(anonymous function) (webpack-internal:///../../../compiler/esm5/compiler.js:27934:27)
    at eval (webpack-internal:///../../../compiler/esm5/compiler.js:28460:22)
    at Array.map (<anonymous>)
    at ViewBuilder._createNodeExpressions (webpack-internal:///../../../compiler/esm5/compiler.js:28459:56)
    at _AstToIrVisitor.visitPropertyWrite (webpack-internal:///../../../compiler/esm5/compiler.js:26550:23)
    at PropertyWrite.visit (webpack-internal:///../../../compiler/esm5/compiler.js:4895:24)
    at convertActionBinding (webpack-internal:///../../../compiler/esm5/compiler.js:26000:45)
    at eval (webpack-internal:///../../../compiler/esm5/compiler.js:28519:22)
    at Array.forEach (<anonymous>)
    at ViewBuilder._createElementHandleEventFn (webpack-internal:///../../../compiler/esm5/compiler.js:28515:18)
    at nodes.(anonymous function) (webpack-internal:///../../../compiler/esm5/compiler.js:27934:27)
    at eval (webpack-internal:///../../../compiler/esm5/compiler.js:28460:22)
    at Array.map (<anonymous>)
    at ViewBuilder._createNodeExpressions (webpack-internal:///../../../compiler/esm5/compiler.js:28459:56)
    at resolvePromise (webpack-internal:///../../../../zone.js/dist/zone.js:824:31)
    at resolvePromise (webpack-internal:///../../../../zone.js/dist/zone.js:795:17)
    at eval (webpack-internal:///../../../../zone.js/dist/zone.js:873:17)
    at ZoneDelegate.invokeTask (webpack-internal:///../../../../zone.js/dist/zone.js:425:31)
    at Object.onInvokeTask (webpack-internal:///../../../core/esm5/core.js:4944:33)
    at ZoneDelegate.invokeTask (webpack-internal:///../../../../zone.js/dist/zone.js:424:36)
    at Zone.runTask (webpack-internal:///../../../../zone.js/dist/zone.js:192:47)
    at drainMicroTaskQueue (webpack-internal:///../../../../zone.js/dist/zone.js:602:35)
    at <anonymous>

我在互联网上搜索并找到相同错误的问题和答案 - 但我没有找到任何适合我的解决方案......这就是为什么这个错误可能会重复但答案似乎与那里的许多不同。

【问题讨论】:

  • 欢迎问答,但请确保您先搜索,还有其他问题和答案;)
  • 如果您将其标记为重复,您不应该向我们提出这是重复的问题吗?

标签: angular


【解决方案1】:

问题是我将 ngModel 变量名重命名为完全相同

[(ngModel)]="adminUsername" #adminUsername="ngModel" 

对我有用的解决方案

[(ngModel)]="adminUsernameText" #adminUsername="ngModel" 

【讨论】:

  • 太棒了..错误消失了。那么如何以不同的名称验证它
  • @PonmariSubramanian - #adminUsername 将在 html 代码中用于验证检查,而 adminUsernameText 是 ts 文件中的变量,其他函数和代码将在那里使用它 - 顺便说一下,adminUsernameText 表示角度的两种方式绑定 - 我希望你明白了
  • 我认为错误消息不包含对问题发生位置的引用很烦人。
  • 那是什么?角度很难!
  • 请注意,当您尝试将值分配给模板中的上下文给定变量时也会出现此错误——类似于let-localVar="outsideVar" 并尝试执行类似于(click)="localVar = !localVar" 的操作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-07
  • 1970-01-01
  • 2019-09-06
  • 2019-04-17
  • 2018-08-27
  • 2020-01-18
相关资源
最近更新 更多