简体   繁体   English

AngularJS:管理DOM的正确方法

[英]AngularJS: Correct Way of Managing DOM

I'm still new to angular and am wondering whats the 'right' way of tackling this problem. 我仍然对角度不熟悉,想知道解决这个问题的“正确”方法是什么。 Suppose the back end has events and attendees . 假设后端有eventsattendees I want an interface to display the events and order by attendees . 我想要一个界面来显示attendees的事件和顺序。 This is easily done in the back end (should I order_by in the database or sort on client side?). 这很容易在后端完成(我应该在数据库中还是在客户端进行order_by排序?)。 The order and number of attendees needs to be constantly updating. attendees的顺序和人数需要不断更新。 Here is what I would do 这是我会做的

app.js

var eventApp = angular.module('eventApp',[]);

eventApp.controller("eventController", ['$scope', '$http', '$interval', function($scope, $http, $interval){
    $scope.events = null;
    var get_events = function(){
        var promise = // make GET call to server, server returns list of events ordered by attendees
        promise.success(function(data, status, headers, config){
            $scope.events = data.questions; // this part seems bad....
        }
    $interval(get_events, 1000);
}])

then in the html, where i want the events to be render 然后在html中,我要在其中呈现事件

EventPage.html
<!-- pretend we are in the appropriate app and controller -->
<div ng-for="event in events">
      <p> {{ events.name }} - {{ events.attendee_count }} </p>
</div>

This just sees wrong, I can see services and directives possibly being helpful but they're still fairly foreign concepts to me. 这只是看错了,我可以看到服务和指令可能会有所帮助,但对我来说它们仍然是陌生的概念。 Wondering what a good approach to this would be, overriding $scope.events each second seems... bad... 想知道有什么好的方法, $scope.events重写$scope.events似乎...很糟糕...

One approach if you don't want to recreate $scope.events every second is to add events to $scope.events if they are new, and delete events if they no longer exist in the response. 如果您不想每秒重新创建$ scope.events,一种方法是将事件添加到$ scope.events中(如果它们是新事件),然后删除事件(如果它们不再存在于响应中)。

But if you are trying to make an HTTP request every second to keep events up-to-date, you may want to consider using a more event-based method of updating the events list, such as a websocket. 但是,如果您试图每秒发出一次HTTP请求以使事件保持最新状态,则可能需要考虑使用一种基于事件的方法来更新事件列表,例如websocket。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM