AngularJS: ng-model 不更新对象,但更新对象中的字段

AngularJS: ng-model not updating object, but updates field in object

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

form.html:

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

details.html:

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

AngularJS details.html 控制器:

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

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

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

ng-include 创建了一个新的作用域,而你的变量是原始变量,所以找不到。

当您将它放在一个对象下时,该对象不会在 ng-include 创建的新子作用域中找到,而是在父作用域中搜索,然后找到并呈现。

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