【问题标题】:AngularJS: ng-model not updating object, but updates field in object [duplicate]AngularJS:ng-model不更新对象,但更新对象中的字段[重复]
【发布时间】:2016-03-21 05:17:36
【问题描述】:

我正在使用 AngularJS 项目,但遇到以下问题。我有一个代码:

form.html:

<form>
  <input ng-model="someVariable" type="text"/>
</form>

details.html:

<div ng-include="'form.html'"></div>
{{ someVariable }}

AngularJS Controller for details.html:

angular.module('SomeModule')
  .controller('DetailsController', function ($scope) {
  $scope.someVariable = {};
});

使用此代码 someVariable 没有显示,但是当我将 ng-model 更改为 someVariable.someField 并尝试显示它时...它可以工作!谁能解释一下为什么?

【问题讨论】:

  • 原型继承

标签: javascript html angularjs angular-ngmodel


【解决方案1】:

是的 - someVariable 是一个原语(布尔、空、未定义、数字、字符串、符号(ECMAScript 6 中的新功能))。当 Angular 搜索要插入的变量时,如果在作用域上找不到它,则只有当该变量不是原始变量时才能查找原型链,例如对象。

ng-include 创建了一个新范围,而您的变量是一个原始变量,因此找不到。

当您将它放在一个对象下时,在ng-include 创建的新子范围内找不到该对象,但会在父范围内搜索它,然后找到并呈现该对象。

更多详细信息 - 阅读Understanding Scopes.

【讨论】:

    猜你喜欢
    • 2015-09-07
    • 2019-04-18
    • 2022-11-01
    • 2014-07-24
    • 2016-05-10
    • 2018-06-09
    • 1970-01-01
    • 2022-01-16
    • 2018-06-11
    相关资源
    最近更新 更多