离子应用程序从sql数据库中检索数据

时间:2016-02-29 18:56:31

标签: angularjs ionic-framework

我正在建立一个离子应用程序来与mysql服务器进行交互。 我试图通过请求一个检索数据并在网络浏览器中以json格式输出的php文件从localhost上的sql数据库中检索数据,在我的应用程序中,http get()执行没有错误,但它不会在应用程序上显示任何内容。 这是我的app.js:

project.controller("myController", function($scope, $http) {
$scope.getData = function() {
    $http.get("http://localhost/server.php")
      .success(function(data) {

        $scope.id = data.Id;
        $scope.name = data.Name;
        $scope.model = data.Model;
        $scope.caryear = data.CarYear;
        $scope.esize = data.EngineSize;
        $scope.etype = data.EngineType;
        $scope.fuel = data.FuelType;
        $scope.doors = data.Doors;
        $scope.price = data.Price;
    })
      .error(function(data) {
        alert("something is wrong");
    })
  }

});

和index.html:

<script type="text/ng-template" id="car2.html">
    <ion-view view-title="Car 2"> 
        <ion-content ng-controller="myController" ng-init="getData()">
            <div>
                <h2>Car 2</h2>
                <br>
                ID: {{id}}
                <br>
                Name: {{name}} {{model}}
                <br>
                Car Year: {{caryear}}
                <br>
                Engine Size: {{esize}}
                <br>
                Engine Type: {{etype}}
                <br>
                Fuel: {{fuel}}
                <br>
                Doors: {{doors}}
                <br>
                Price: {{price}}
            </div>
        </ion-content>
    </ion-view>
</script>

php文件的输出:

[{"Id":"1","Name":"BMW","Model":"M5","CarYear":"2005","EngineSize":"5.0","EngineType":"V10","FuelType":"Petrol","Doors":"4","Price":"25000.00"}]

我有Access Allow Origin的错误,但我在php文件中添加了必需的标题,这些都解决了。 任何有关数据未在应用程序中显示的建议,请提前感谢。

1 个答案:

答案 0 :(得分:1)

在角度范围内定义模型时,请遵循点规则

$scope.model = {};

所有属性为$ scop.model lik

$scope.model = data; //is sufficient

详细answer here