Angular.js在长轮询更改监视值时更新ng类
Angular.js updating ng-class when long polling changes a watched value
使用angular.js
我正在长时间轮询服务器,并且每当build_tag
值增加时,我都希望更新视图中的一个类为"updated"的元素。
因此,当catalog.products[??].build_tag
发生更改时,我正在努力寻找一种优雅的方法来添加更新的类。
<div ng-controller="MyAwesomeController as env">
<div class="product-wrapper" ng-class="{'error': product.error, 'updated': HELP!! }" ng-repeat="product in env.catalog">
<!-- show stuff in cool and interesting ways -->
环境控制器:
app.controller('EnvironmentController', ['$http', '$interval', function($http, $interval) {
var vm = this;
var pollingInterval = 5000;
vm.catalog = {
// only showing 2 products for the
// sake of brevity. I normally have dozens
// of products
products: [{
name: 'widget1',
error: false,
build_tag: 7
}, {
name: 'widget2',
error: false,
build_tag: 5
}]
};
function fetchData() {
$http.get('/builds.json').then(function (resolved) {
if (resolved.status === 200) {
vm.catalog = angular.merge(vm.catalog, resolved.data);
}
}, function (rejected) {
... do error stuff
});
}
$interval(fetchData, pollingInterval);
... more controller stuff
这有道理吗?当我轮询服务器时,我会更新vm.config
。我希望每个产品包装器都能看到它是build_tag
。如果build_tag
已经更改,我想将updated
类添加到视图中的相应元素中。
您总是可以编写一个指令来添加类。
.directive('buildTagUpdated', [function() {
return {
link: function buildTagUpdatedLink(scope, element, attributes) {
scope.$watch('buildTag', function(newValue, oldValue) {
if (newValue !== oldValue) {
/**
* you can use element methods here
* see https://docs.angularjs.org/api/ng/function/angular.element
*/
}
});
},
restrict: 'A',
scope: {
buildTag: '='
}
};
}]);
然后在要更新的元素上,只需添加build-tag-updated="product.build_tag"
相关文章:
- 监视函数从服务返回不起作用,但作用域函数起作用
- Javascript循环不会自我更新
- 添加文字和评论功能更新Div
- AngularJS:ng之后,重复$scope值未按预期更新
- 如何通过数组更新角度子范围
- Ajax聊天消息重复而不仅仅是更新
- 通过CSV文件上载更新数据库表
- 平均值:无法将数据更新到数据库
- 在节点脚本中,如何监视文件的更改并获取更新的内容
- AngularJS:如何重新启动控制器以更新要监视的数据源模型
- 更新一个控制器中的模型,该模型被监视并位于不同的包中,并由另一个控制器使用
- 从指令更新作用域,并使用另一个指令进行监视
- 更新Angular Directive的值时出现问题's模型,同时从表单的监视进行更新
- angular ui-router中的Named视图没有更新,尽管它被监视了
- Angular.js在长轮询更改监视值时更新ng类
- Javascript中的循环/监视对象的更新
- Angular ng-repeat的最佳实践:只监视特定迭代中的更新
- 将作用域对象传递给可以监视和更新它的服务
- 监视嵌套在传递到角度组件的多个对象中的更新
- 监视不在$scope上的变量以更新在$scop上的变量