- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在将自定义指令升级到新的 component architecture 。我读到组件不支持观察者。它是否正确?如果是这样,您如何检测对象的变化?对于一个基本示例,我有自定义组件 myBox
,它有一个子组件 game ,并绑定(bind)在 game 上。如果游戏组件中有更改游戏,我如何在 myBox 中显示警报消息?我知道有 rxJS 方法是否可以纯粹以 Angular 来做到这一点?我的JSFiddle
JavaScript
var app = angular.module('myApp', []);
app.controller('mainCtrl', function($scope) {
$scope.name = "Tony Danza";
});
app.component("myBox", {
bindings: {},
controller: function($element) {
var myBox = this;
myBox.game = 'World Of warcraft';
//IF myBox.game changes, show alert message 'NAME CHANGE'
},
controllerAs: 'myBox',
templateUrl: "/template",
transclude: true
})
app.component("game", {
bindings: {game:'='},
controller: function($element) {
var game = this;
},
controllerAs: 'game',
templateUrl: "/template2"
})
HTML
<div ng-app="myApp" ng-controller="mainCtrl">
<script type="text/ng-template" id="/template">
<div style='width:40%;border:2px solid black;background-color:yellow'>
Your Favourite game is: {{myBox.game}}
<game game='myBox.game'></game>
</div>
</script>
<script type="text/ng-template" id="/template2">
<div>
</br>
Change Game
<textarea ng-model='game.game'></textarea>
</div>
</script>
Hi {{name}}
<my-box>
</my-box>
</div><!--end app-->
最佳答案
这个答案概述了在不使用 watchers. 的情况下编写 AngularJS 1.5 组件的五种技术
ng-change
Directive $onChanges
Life-cycle Hook $doCheck
Life-cycle Hook ng-change
指令what alt methods available to observe obj state changes without using watch in preparation for AngularJs2?
您可以使用ng-change
指令对输入更改使用react。
<textarea ng-model='game.game'
ng-change="game.textChange(game.game)">
</textarea>
要将事件传播到父组件,需要将事件处理程序添加为子组件的属性。
<game game='myBox.game' game-change='myBox.gameChange($value)'></game>
JS
app.component("game", {
bindings: {game:'=',
gameChange: '&'},
controller: function() {
var game = this;
game.textChange = function (value) {
game.gameChange({$value: value});
});
},
controllerAs: 'game',
templateUrl: "/template2"
});
在父组件中:
myBox.gameChange = function(newValue) {
console.log(newValue);
});
这是 future 的首选方法。 AngularJS的使用策略$watch
不可扩展,因为它是一种轮询策略。当数量$watch
听众达到 2000 左右时,UI 变得迟缓。 Angular 2 中的策略是使框架更具 react 性并避免放置 $watch
上$scope
.
$onChanges
生命周期钩子(Hook)在版本 1.5.3 中,AngularJS 添加了 $onChanges
$compile
的生命周期 Hook 服务。
来自文档:
The controller can provide the following methods that act as life-cycle hooks:
- $onChanges(changesObj) - Called whenever one-way (
<
) or interpolation (@
) bindings are updated. ThechangesObj
is a hash whose keys are the names of the bound properties that have changed, and the values are an object of the form{ currentValue: ..., previousValue: ... }
. Use this hook to trigger updates within a component such as cloning the bound value to prevent accidental mutation of the outer value.— AngularJS Comprehensive Directive API Reference -- Life-cycle hooks
$onChanges
钩子(Hook)用于对组件的外部更改使用react <
单向绑定(bind)。 ng-change
指令用于传播 ng-model
的更改组件外部的 Controller &
绑定(bind)。
$doCheck
生命周期钩子(Hook)在版本 1.5.8 中,AngularJS 添加了 $doCheck
$compile
的生命周期 Hook 服务。
来自文档:
<小时/>The controller can provide the following methods that act as life-cycle hooks:
$doCheck()
- Called on each turn of the digest cycle. Provides an opportunity to detect and act on changes. Any actions that you wish to take in response to the changes that you detect must be invoked from this hook; implementing this has no effect on when$onChanges
is called. For example, this hook could be useful if you wish to perform a deep equality check, or to check a Date object, changes to which would not be detected by Angular's change detector and thus not trigger$onChanges
. This hook is invoked with no arguments; if detecting changes, you must store the previous value(s) for comparison to the current values.— AngularJS Comprehensive Directive API Reference -- Life-cycle hooks
require
进行组件间通信指令可以require其他指令的 Controller 以实现彼此之间的通信。这可以在组件中通过为 require 提供对象映射来实现。属性(property)。对象键指定属性名称,所需的 Controller (对象值)将在该属性名称下绑定(bind)到所需组件的 Controller 。
app.component('myPane', {
transclude: true,
require: {
tabsCtrl: '^myTabs'
},
bindings: {
title: '@'
},
controller: function() {
this.$onInit = function() {
this.tabsCtrl.addPane(this);
console.log(this);
};
},
templateUrl: 'my-pane.html'
});
有关详细信息,请参阅AngularJS Developer Guide - Intercomponent Communicatation
<小时/>What about in a situation where you have a Service that's holding state for example. How could I push changes to that Service, and other random components on the page be aware of such a change? Been struggling with tackling this problem lately
使用 RxJS Extensions for Angular 构建服务.
<script src="//unpkg.com/angular/angular.js"></script>
<script src="//unpkg.com/rx/dist/rx.all.js"></script>
<script src="//unpkg.com/rx-angular/dist/rx.angular.js"></script>
var app = angular.module('myApp', ['rx']);
app.factory("DataService", function(rx) {
var subject = new rx.Subject();
var data = "Initial";
return {
set: function set(d){
data = d;
subject.onNext(d);
},
get: function get() {
return data;
},
subscribe: function (o) {
return subject.subscribe(o);
}
};
});
然后只需订阅更改即可。
app.controller('displayCtrl', function(DataService) {
var $ctrl = this;
$ctrl.data = DataService.get();
var subscription = DataService.subscribe(function onNext(d) {
$ctrl.data = d;
});
this.$onDestroy = function() {
subscription.dispose();
};
});
客户可以使用 DataService.subscribe
订阅更改生产者可以通过 DataService.set
推送更改。
关于AngularJS 1.5+ 组件不支持观察者,解决方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35534479/
AngularFire2 文档演示了以下将观察者绑定(bind)到您的 Firebase 可观察对象的模式: this.item = af.database.object('/item'); {{ (
我正在使用观察者模式和 BlockingQueue 添加一些实例。现在在另一种方法中,我正在使用队列,但似乎 take() 一直在等待,即使我这样做: /** {@inheritDoc} */ @Ov
我遇到问题 - 创建具有不同属性的订单。在我的工作案例中,用户有大约 10 个不同的属性,每个属性有 3-4 个变体。 我正在使用 Angular js,并编写了两个函数: 二传手 $scope
我尝试将额外的产品添加到购物车。我为此创建了一个观察者。 getEvent()->getControllerAction()->getFullActionName() == 'checkout_car
我尝试将额外的产品添加到购物车。我为此创建了一个观察者。 getEvent()->getControllerAction()->getFullActionName() == 'checkout_car
假设我想创建一组基于类型的观察者。也就是说,当他们收到事件通知时,他们会被告知其中一个参数的类型,然后根据是否可以对该类型进行操作来决定是否采取行动。 有没有简单的方法可以做到这一点?我认为这对于泛型
快速提问。 当 setChanged() 和 notifyObservers() 被调用并且 Observer.update() 运行时,它是否在一个新的 Thread,还是在同一个Thread中?
我必须为可观察对象和观察者创建代码。正如您所知,每个观察者都有自己的“更新”方法。 在我的可观察代码中,我使用了“notifyObservers”方法。它的参数是一个字符串数组,其中包含有关我的项目的
是否有一种设计模式可以形成“复合”观察者/可观察对象? 我的意思是我有一个可观察的 A 通知它的听众一些变化。 每个监听器也是一个可观察对象,并通知自己的监听器(在它执行的某些操作中,哪个操作是由第一
我对以下代码为何不起作用感到有点困惑: MutableLiveData mutableTest = new MutableLiveData<>(); MediatorLiveData mediator
场景:我有两个名为 FirstFragment 的 fragment 和 UnitFragment .我来自 FirstFragment至UnitFragment选择一个单位返回FirstFragme
我想了解如何 平面 map 作品。我知道这是处理 Observable> 的一种方式。 无论如何,我正在测试它的行为并陷入困境: let plusDom = document.querySelecto
我有 public class mammal implements Imammle { public String getName() { return "Mammal";
如果我使用 KVO 来观察我的播放器项目,如下所示: AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
我想在正在使用的变量上使用观察者,这是我的代码: const get = Ember.get; uploader:Ember.inject.service('uploader'), progressC
我一直在使用Laravel Observers每当我的模型被删除/更新/保存时处理我的应用程序逻辑。我注意到在某些情况下这些观察者不会被触发。例如,如果我使用 Model::where(active,
我有一个工作人员服务来发送一些简单的通知。但现在,我想在发送之前检查数据库中的一些数据。 但是,我不能在工作线程中使用观察者,它必须在主线程中。我有一个错误类型 java.lang.IllegalSt
我有 2 个字段:美元和日元。当我更改美元字段时,我想在日元字段中显示转换后的数字,反之亦然。 我的问题是这样的: 如果我有 2 个带有观察者的字段,例如: addTextChangedListene
在 View Controller 中,我创建了一个 subview 数组,可以随时从父 View 中删除,因此它们的生命周期比 View Controller 的生命周期短。 创建它们时,我几乎这样
如何在另一个线程上运行 RxJava,因为主线程上的工作太多。 我在一个方法内运行观察者,下面是代码 fragment : public void updatePie() { RxJavaPlugin
我是一名优秀的程序员,十分优秀!