如何重新从编程服务器/刷新模型数据? [英] How to reload / refresh model data from the server programmatically?

查看:161
本文介绍了如何重新从编程服务器/刷新模型数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有最基本的新手AngularJS问题,原谅我的无知:我怎么通过刷新code的模式?我敢肯定,它是介于回答多次,但我根本进不了
找到它。
我在这里观看精彩影片 http://egghead.io 并快步走到了教程,但还是我觉得我失去了一些东西很基本的。

I have the most basic "newbie" AngularJS question, forgive my ignorance: how do I refresh the model via code? I'm sure it's answered multiple times somewhere, but I simply couldn't find it. I've watched some great videos here http://egghead.io and went quickly over the tutorial, but still I feel I'm missing something very basic.

我发现一个相关的例子这里 $ route.reload( )),但我不知道我知道如何在这个例子中使用如下

I found one relevant example here ($route.reload()) but I'm not sure I understand how to use it in the example below

function PersonListCtrl($scope, $http) {
  $http.get('/persons').success(function(data) {
    $scope.persons = data;
  });
}

的index.html

...
<div>
    <ul ng-controller="PersonListCtrl">
        <li ng-repeat="person in persons">
            Name: {{person.name}}, Age {{person.age}}
        </li>
    </ul>
</div>
...

这一切都工作得非常好,每个页面重新加载时间,我看到的人名单如预期

This all works amazingly well, each time the page is reloaded I see the list of people as expected


  1. 比方说,我想实现一个刷新按钮,我怎么告诉模型以编程方式加载?

  2. 我怎样才能访问的模式?这似乎是角神奇实例我的控制器的一个实例,但我如何得到我的手呢?

  3. 修改加了第三个问题,同#1,但怎么能够通过JavaScript做纯粹?

  1. Let's say I want to implement a refresh button, how do I tell the model to reload programmatically?
  2. How can I access the model? it seems Angular is magically instantiating an instance of my controller, but how do I get my hands on it?
  3. EDIT added a third question, same as #1 but how can it be done purely via JavaScript?

我敢肯定,我失去了一些东西基本的,但花费一个小时试图数字出来之后,我认为值得的问题。请让我知道,如果它的复制和我将关闭+链接到它。

I'm sure I'm missing something basic, but after spending an hour trying to figure it out, I think it deserves a question. Please let me know if it's duplicate and I'll close + link to it.

推荐答案

您是你自己走了一半。要实现刷新,你只是换你已经在上范围的功能:

You're half way there on your own. To implement a refresh, you'd just wrap what you already have in a function on the scope:

function PersonListCtrl($scope, $http) {
  $scope.loadData = function () {
     $http.get('/persons').success(function(data) {
       $scope.persons = data;
     });
  };

  //initial load
  $scope.loadData();
}

然后在您的标记

<div ng-controller="PersonListCtrl">
    <ul>
        <li ng-repeat="person in persons">
            Name: {{person.name}}, Age {{person.age}}
        </li>
    </ul>
   <button ng-click="loadData()">Refresh</button>
</div>

至于你的访问模式,所有你需要做的是获得了$ scope.persons在你的控制器阵列:

As far as "accessing your model", all you'd need to do is access that $scope.persons array in your controller:

例如(只是puedo code)在你的控制器:

for example (just puedo code) in your controller:

$scope.addPerson = function() {
     $scope.persons.push({ name: 'Test Monkey' });
};

然后就可以使用,在您的视图或任何你想做的事情。

Then you could use that in your view or whatever you'd want to do.

这篇关于如何重新从编程服务器/刷新模型数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆