javascript 从数据库获取数据到角度js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24319606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:34:24  来源:igfitidea点击:

Getting data from database to angular js

javascriptphpjsonangularjshttp

提问by Juan Camilo Guarin P

i'm new to Angular JS and i've been learning from codeschool.

我是 Angular JS 的新手,我一直在从 Codeschool 学习。

I have this problem i can't figure out how to solve: I want to get data from a php file that i'm working on but first i wanted to make a short example because something just doesn't make sense to me, and is that can never retreive the information that has the php file to the angular controller.

我有这个问题,我不知道如何解决:我想从我正在处理的 php 文件中获取数据,但首先我想做一个简短的例子,因为有些东西对我来说没有意义,并且是永远无法将具有 php 文件的信息检索到角度控制器。

I have uploaded it on a jsfiddleyou can check out if you want

我已将其上传到jsfiddle,您可以查看是否需要

Here's the html, it's pretty basic:

这是 html,它非常基本:

<div ng-app="app">
    <div ng-controller="MyController as c">
         <h1>{{c.title}}</h1>
        <p>Controller trial: {{c.content}}</p>
    </div>
</div>

And here the javascript source:

这里是 javascript 源代码:

var app = angular.module("app", []);
app.controller("MyController", ['$http', function ($http) {
    this.title = "My Title";
    var filecontent = "Data should be replaced";
    $http.get("http://www.otherwise-studios.com/example.php")
        .success(function (data) {
            filecontent = data;
            //I don't know why data is not loaded here :S
        });
    this.content = filecontent;
}]);

Finally, this is the output i'm getting:

最后,这是我得到的输出:

My Title

Controller trial: Data should be replaced

If you visit the linkfrom which i'm retreiving the information you should see this output "You connected to my php file", but as i said before, the data seems to never get updated to the variable:

如果您访问我从中检索信息的链接,您应该看到此输出“您已连接到我的 php 文件”,但正如我之前所说,数据似乎永远不会更新到变量:

this.content

Thank you so much for all your help

非常感谢您的帮助

Juan Camilo Guarin P

胡安·卡米洛·瓜林 P

采纳答案by Miraage

Data isn't loaded here, because initially "content" is primitive. You have two ways: init "content" as object or write smth like this:

此处不加载数据,因为最初“内容”是原始的。您有两种方法:将“内容”初始化为对象或像这样编写 smth:

var filecontent = "la la la",
    that = this;

$http.get("http://www.otherwise-studios.com/example.php")
    .success(function (data) {
        that.content = data;
    });

回答by Code Whisperer

app.controller("MyController", ['$http', '$scope'  function ($http,$scope) {
this.title = "My Title";
var filecontent = "Data should be replaced";
$http.get("http://www.otherwise-studios.com/example.php")
    .success(function (data) {
        filecontent = data;
        $scope.content = filecontent; // must be within success function to automatically call $apply
    });

}]);