最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting data from database to angular js - Stack Overflow

programmeradmin0浏览0评论

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 retrieve 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(".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 retrieving 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

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 retrieve 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./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 retrieving 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

Share Improve this question edited Jun 18, 2020 at 6:04 Alyona Yavorska 5792 gold badges14 silver badges20 bronze badges asked Jun 20, 2014 at 3:47 Juan Camilo Guarin PJuan Camilo Guarin P 1831 gold badge2 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

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

}]);
发布评论

评论列表(0)

  1. 暂无评论