简体   繁体   English

在angularjs页面中获取mysql结果

[英]get mysql results in an angularjs page

I am very new to Angular, and I am really needing to figure this out quick ... I am just trying to pull info over from a mysql table into my angular page. 我是Angular的新手,我真的需要快速解决这个问题……我只是想将信息从mysql表中提取到我的Angular页面中。 I don't know where I am wrong, so I don't know what to fix. 我不知道我在哪里错了,所以我不知道要解决什么。 Here is the html: 这是html:

<md-content class="md-padding" layout="row" layout-wrap>
    <md-card flex="40">
        <md-card-content>

            <div class="classified-info" ng-repeat="classified in classifieds">
                <h2 class="md-title">{{ classified.subnet }}</h2>
                <h2 class="md-title"> {{ classified.ipAdd }} </h2>
            </div>

        </md-card-content>
    </md-card>

    <md-card flex="40">
        <md-card-content>
            <div class="classified-info">
                <h2 class="md-title"> {{ classified.ipAdd }} </h2>
            </div>
        </md-card-content>
    </md-card>
</md-content>

here is the controller: 这是控制器:

(function() {

"use strict";

angular
    .module("ngClassifieds")
    .controller("classifiedsCtrl", function($scope, $http) {

        $http.get('data/get_results.php').then(function(classifieds) {
            $scope.classifieds = classifieds.data;
        });

    });
})();

and here is the php: 这是PHP:

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$conn = new mysqli("HOST", "UN", "PW", "TBL");

$result = $conn->query("SELECT * FROM ip_addresses");

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "") {$outp .= ",";}
$outp .= '{"subnet":"'  . $rs["id_sub"] . '",';
$outp .= '"ipAdd":"'   . $rs["ip_add"]        . '",';
$outp .= '"subMask":"'. $rs["ip_mask"]     . '"}';
}
$outp ='{"classifieds":['.$outp.']}';
$conn->close();

echo($outp);
?>

(function() { (function(){

"use strict"; “使用严格”;

the probmlem is here: 问题在这里:

angular
    .module("ngClassifieds")
    .controller("classifiedsCtrl", function($scope, $http) {

        $http.get('data/get_results.php').then(function(classifieds) {
            $scope.classifieds = classifieds**.data**;//data is not defined in JSON response. the correct access is  classifieds.classifieds
        });

    });
})();

In addition to Danilo's answer, I don't see where you are connecting the app and controller to the html. 除了Danilo的答案外,我看不到您将应用程序和控制器连接到html的位置。

Surround your html with: 用以下内容包围您的html:

<div ng-app="ng-classifieds">
    <div ng-controller="classifiedsCtrl">


        // Your html code here.


    </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM