javascript-推送后未更新Angular ng-repeat
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-推送后未更新Angular ng-repeat,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3594字,纯文字阅读大概需要6分钟。
内容图文
![javascript-推送后未更新Angular ng-repeat](/upload/InfoBanner/zyjiaocheng/663/763e8726547f4f6dbc78e1af95f46323.jpg)
我一直在努力与Angular一起努力,了解范围和不了解范围.
将新的“电线”插入我的阵列后,ng-repeat不会更新.我一直在阅读,可能是因为我添加数组时超出了我的数组范围(建议我可以添加到其他数组中),但事实并非如此吗?
这是我的代码
<body ng-app="wireApp" ng-controller="AddWireController">
<header>
<form role="form" class="form">
<div class="form-group">
<input type="text" placeholder="Description" class="form-control" name="wireDescription" ng-model="wire.description">
<input type="text" placeholder="URL" class="form-control" name="wireURL" ng-model="wire.URL">
<input type="text" placeholder="Tags" class="form-control" name="wireTags" ng-model="wire.tags">
<input type="text" placeholder="Groups" class="form-control" name="wireGroups" ng-model="wire.groups">
</div>
<button class="btn btn-primary btn-block" ng-click="addwire(wire)">Add+</button>
</form>
</header>
<div id="timeline" ng-controller="ListWireController">
<div ng-repeat="wire in wires">
<div class="timeline-entry">
<div class="timeline-stat">
<div class="timeline-icon bg-info"><i class="fa fa-envelope fa-lg"></i></div>
<div class="timeline-time">{{ wire.linLastTouched }}</div>
</div>
<div class="timeline-label">
<h4 class="text-info text-lg">{{ wire.linDescription }}</h4>
<p>{{ wire.tags }}</p>
</div>
</div>
</div>
</div>
</body>
这是javascript(角度):
var wireApp = angular.module('wireApp', []);
//Parent controller
wireApp.controller('AddWireController', ['$scope', function($scope) {
$scope.addwire = function(wire) {
$.post('/wire/create', wire, function(data) {
$scope.$broadcast('addwire', data); //emit to children
});
};
}]);
//Child of AddWireController
wireApp.controller('ListWireController', ['$scope', function($scope) {
$scope.wires = [];
$scope.getwireByGroup = function(groupID) {
$.get('/wire/grpID=' + groupID, function(data) {
$.each(data.wires, function(index, key){
var newKey = key;
newKey.linLastTouched = jQuery.timeago(newKey.linLastTouched);
$scope.wires.push(newKey);
});
});
};
$scope.$on('addwire', function(event, mass) {
$scope.addwire(mass);
});
$scope.addwire = function(wire){
$scope.$apply(function() {
$scope.wires.push(wire);
});
}
//init data
$scope.getwireByGroup(0);
}]);
附加问题:
鉴于我使用$broadcast的方式,这迫使我在两个控制器之间建立关系,但是如果我不想在两者之间建立关系怎么办?我需要使用一家有前途的工厂吗?您能为我提供一个示例代码吗?
编辑:
首先,我要非常感谢Simon H和Keval Bhatt,因为他们帮助我理解了问题,而不仅仅是帮助我找到了解决方案.
这是工作代码(角度):
var wireApp = angular.module('wireApp', []);
wireApp.factory('wireFactory', function($http){
var wires = [];
return {
getwireByGroup: function(groupID){
$http.get('/wire/grpID=' + groupID)
.success(function(data) {
$.each(data.wires, function(index, key){
var newKey = key;
newKey.linLastTouched = jQuery.timeago(newKey.linLastTouched);
wires.push(newKey);
});
});
return wires;
},
addwire: function(wire){
$http.post('/wire/create', wire)
.success(function(data) {
wires.push(data);
});
}
}
});
//Parent controller
wireApp.controller('AddWireController', function($scope, wireFactory) {
$scope.addwire = function(wire) {
wireFactory.addwire(wire);
};
});
//Child of AddwireController
wireApp.controller('ListWireController', function($scope, wireFactory) {
$scope.wires = [];
$scope.getwireByGroup = function(groupID) {
$scope.wires = wireFactory.getwireByGroup(groupID);
};
$scope.getwireByGroup(0);
});
解决方法:
原因是您将Angular与jQuery混合使用.当您使用Angular函数(例如$http)时,在接收到数据后,它会执行“摘要”操作,以更新视图.但是Angular不知道$.get会接收到数据,因此在这种情况下不会更新.
我的建议是放弃jQuery,转而使用角度方法.但是如果不能,那么每次您需要屏幕更新时,都添加一个$scope.$apply()
内容总结
以上是互联网集市为您收集整理的javascript-推送后未更新Angular ng-repeat全部内容,希望文章能够帮你解决javascript-推送后未更新Angular ng-repeat所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。