我正在使用 AngularJS 1.5 并使用 '$emit' 向父 Controller 发送事件以刷新父 Controller 数据。在 '$On' 上,我编写了刷新父 Controller 数据的逻辑。
现在,父 Controller 数据正在更新,但之后无法为触发“$emit”的子 Controller 绑定(bind)数据。
我尝试使用'$apply',但它说'$digest'已经在进行中。我还使用 Batrang 工具查看数据,它显示该页面包含所有这些数据,但未显示在 UI 上。
谁能告诉我如何强制 Angular 将这些数据与页面上已有的 HTML 控件绑定(bind)。
我无法将示例代码放在这里,因为它是一个实时项目,我必须创建一个示例项目来复制该问题。即使如果没有示例代码很难回答我的查询,那么我会在一天内将示例代码放在 Plunker 上。
最佳答案
根据 Angular 文档,有两种在 HTML 中声明 Controller 的方法:
one binds methods and properties directly onto the controller using
ng-controller="SettingsController1 as settings"
one injects
$scope
into the controller:ng-controller="SettingsController2"
The second option is more common in the Angular community, and is generally used in boilerplates and in this guide. However, there are advantages to binding properties directly to the controller and avoiding scope.
Using
controller as
makes it obvious which controller you are accessing in the template when multiple controllers apply to an element. If you are writing your controllers as classes you have easier access to the properties and methods, which will appear on the scope, from inside the controller code. Since there is always a.
in the bindings, you don't have to worry about prototypal inheritance masking primitives.
因此,您始终可以使用 controller as
在子作用域中引用父作用域:
<div ng-controller="parentController as parent">
<span>{{parent.title}}</span>
<div ng-controller="childController as child">
<span>{{parent.title}}</span>
<span>{{child.title}}</span>
</div>
</div>
关于javascript - 使用 $emit 刷新父数据时,AngularJS 子 Controller 数据未加载到 UI 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39897213/