gpt4 book ai didi

php - angular JS刷新一个http

转载 作者:行者123 更新时间:2023-11-30 12:57:36 24 4
gpt4 key购买 nike

我正在尝试与联系人制定议程,我刚刚开始学习 AngularJS。到目前为止,我做了一个 php 生成一个 JSON,将它加载到 Angular Controller 上,然后在 html 上显示它。

这是 Angular 代码

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

Agenda.controller('Contacts', function($scope, $http) {
$http.get('php/contacts.php').success(function(data) {
$scope.jsonContacts = data;
});
});

这是 HTML

<section class="agenda" ng-app="Agenda">
<ul ng-controller="Contacts">
<li ng-repeat="contact in jsonContacts">
<div class="col1">{{contact.contact_id}}</div>
<div class="col2">{{contact.contact_firstname + ' ' + contact.contact_lastname}}</div>
<div class="col3">{{contact.contact_phone}}</div>
<div class="col4">{{contact.contact_email}}</div>
</li>
</ul>

<a>Refresh</a>
</section>

到目前为止一切顺利,但现在我试图在按下刷新时刷新列表的内容,但我不知道该怎么做。如果我在此代码中也做错了什么,请告诉我。

提前谢谢你,丹尼尔!

最佳答案

您已经完成了一半以上!这是对您所拥有的东西的一些细微调整,应该可以解决问题。将您的 $http 调用转换为我们可以重复调用的函数 - refresh():

Agenda.controller('Contacts', function($scope, $http) {
$scope.refresh = function() {
$http.get('php/contacts.php').success(function(data) {
$scope.jsonContacts = data;
});
}

// call it initially
$scope.refresh();
});

然后简单地使用 ng-click 调用我们在上面添加的新的 refresh() 函数:

<section class="agenda" ng-app="Agenda">
<ul ng-controller="Contacts">
<li ng-repeat="contact in jsonContacts">
<div class="col1">{{contact.contact_id}}</div>
<div class="col2">{{contact.contact_firstname + ' ' + contact.contact_lastname}}</div>
<div class="col3">{{contact.contact_phone}}</div>
<div class="col4">{{contact.contact_email}}</div>
</li>
</ul>

<a ng-click="refresh()">Refresh</a>
</section>

关于php - angular JS刷新一个http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18499942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com