drwkqwa82140 2014-06-20 03:47 采纳率: 0%
浏览 36
已采纳

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

i'm new to Angular JS and i've been learning from 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.

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

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

<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:

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 link from 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:

this.content

Thank you so much for all your help

Juan Camilo Guarin P

  • 写回答

2条回答 默认 最新

  • dongshimao7115 2014-06-20 03:51
    关注

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

    var filecontent = "la la la",
        that = this;
    
    $http.get("http://www.otherwise-studios.com/example.php")
        .success(function (data) {
            that.content = data;
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿