【问题标题】:GAE and Angular: How to update data of a HTML JINJA templateGAE 和 Angular:如何更新 HTML JINJA 模板的数据
【发布时间】:2017-10-18 23:14:40
【问题描述】:

我在使用 GAE 和 Angular 时遇到了关于在 HTML 模板上动态更新数据的问题。 我想要的是当一个新用户插入系统时,它必须自动显示在列出系统所有用户的页面上。 我不知道该怎么做。我阅读了有关 Angular 的文章,它似乎是一个在 HTML 网页上动态更新数据的好框架。 我使用 JINJA 获取页面并通过查询存储所有用户的 NDB 数据存储来填充模板。 这是“处理页面”的 webapp 处理程序的代码:

class TestA(BaseHandler):   
   def get(self):   
    template = JINJA_ENVIRONMENT.get_template('templates/Ang2.html')
    Users=datastore._retrieve_all_user()
    w = self.response.write   
    w(template.render({'Users': Users}))

当我插入新用户时,会显示包含所有用户的页面,同时在另一个选项卡中不会显示更新,但如果我手动刷新页面,则会显示新用户。

所以,我想要的是自动获得相同的结果。 关键是我想定期刷新 HTML 页面,以显示最终的新用户。

所以我专注于 Angular,尤其是 $interval 和 $route.reload(),但我试图做出一些可行的东西,但我什么都没有。

这是模板的代码:

<!doctype html>
<html ng-app="demoApp">
<head>
    <title>Hello from a template page!</title>
    <link rel="stylesheet" type="text/css" href="/css/helloworld.css">
</head>
<body>
    <h1>Hello world HTML template.</h1>
    <div data-ng-controller="MyController"> 
    <h3>Elenco utenti:</h3>
    <ul>
    {% for User in Users %}
<li><b>{{User.username}}</b>: {{User.email}} </li>
{% endfor %}
    </ul>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>    
<script>
var demoApp = angular.module("demoApp", []);
myapp.controller("MyController", function($scope, $interval){

$interval(callAtInterval, 5000);


});

function callAtInterval($route) {
$route.reload();

}
</script>   

</body>

任何提示如何做到这一点? PS:我知道这个解决方案并不优雅,但我选择了这个解决方案,因为我需要知道如何去做,因为它对我的 webapp 功能开发的后续步骤很有用

【问题讨论】:

标签: javascript html angularjs google-app-engine


【解决方案1】:

不要刷新页面来更新数据。 Angular 的本意是在不刷新的情况下更新数据。

创建一个 API 作为数据存储操作的接口。使用 Angular 在此 API 上执行 Promise 请求,根据 Promise 返回更改范围。

首先阅读有关 Angular 工作原理的文档,然后学习如何创建 API。

角度 https://docs.angularjs.org/guide/concepts

API 假设您使用的是 Python https://www.codementor.io/sagaragarwal94/building-a-basic-restful-api-in-python-58k02xsiq

【讨论】:

    猜你喜欢
    • 2012-09-07
    • 1970-01-01
    • 2023-03-09
    • 2019-03-18
    • 1970-01-01
    • 2018-10-28
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多