angular - 错误 : Cannot assign to a reference or variable! Angular 4

标签 angular

<分区>

添加一些新的 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>

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

最佳答案

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

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

对我有用的解决方案

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

关于angular - 错误 : Cannot assign to a reference or variable! Angular 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48021581/

相关文章:

javascript - 从 clientLeft、clientTop、offsetLeft 和 offsetTop 获取元素位置的错误值

javascript - 如果在下拉菜单内发生单击,则阻止下拉菜单关闭

angular - 使用 angular2 中的 'xlsx' 导出到 CSV 文件

javascript - Angular Material Checkbox 不会改变选中的状态

Angular2 获取被点击的元素id

CSS 未显示在谷歌浏览器的覆盖选项卡中

javascript - Angular 2 中的字符串插值不显示更新值

angular - 使用 angular-cli-ghpages 将 Angular4 部署到 ghpages 时出错

angular - 从rest api打印JSON对象的第一个字符 - Angular 2

node.js - 当服务器向特定客户端发出事件(不同的 Angular2 服务)时,不要将 socket.io-client 与 Angular2 一起使用