简体   繁体   English

AngularJS $ http.get产品

[英]AngularJS $http.get products

i created my new store in angularjs. 我在angularjs中创建了新商店。 In there i have a function called store, where it gets all my products. 在那儿,我有一个名为store的功能,该功能可以存储我所有的产品。 In the start i just wrote all the products in there, but now i want to get them from a mysql database. 首先,我只是在那里写了所有产品,但现在我想从mysql数据库中获取它们。 I think that the problem is something in the $http.get, but dont know what. 我认为问题出在$ http.get中,但不知道是什么。

Before my function looked like this: 在我的函数看起来像这样之前:

function store() {
    this.products = [
        { num: 1, code: 'TEST', category: 'Diverse', name: 'TEST', src: "test.png", description: 'info her', price: 2, cal: 10 }
    ];
}

Which worked. 哪个有效。 I created the code below, with the $http.get, and that code dont show anything in my store: 我使用$ http.get创建了以下代码,该代码在我的商店中不显示任何内容:

function store() {

    $http.get('products.php')
        .success(function(data) {
            this.products = data;
        });

}

My products.php file echo this and it does encode to json: 我的products.php文件回显了这一点,并且确实将其编码为json:

[{"num":"1","code":"TET","category":"Diverse","name":"Test","src":"test.png","description":"Test","price":"5","cal":"10"}]

products.php file: products.php文件:

require('scripts/connect.php');

$statement = $conn->prepare("select * from products");
$statement->execute();

while($row = $statement->fetch(PDO::FETCH_ASSOC)){

  foreach($row as $key => $value) {
    $prod[$key] = $value;
  }

  $main_arr[] = $prod;
}

$json = json_encode($main_arr); 
echo $json;

Extra code for help: 额外的帮助代码:

controller.js controller.js

function storeController($scope, $routeParams, DataService) {
    // get store and cart from service
    $scope.store = DataService.store;
    $scope.cart = DataService.cart;
}

app.js app.js

storeApp.factory("DataService", function () {

    // create store
    var myStore = new store();
});

make sure you've properly injected the $http module into your controller (or service, however you're going about it). 确保已将$http模块正确注入到控制器(或服务,但是您正在使用它)中。

var myApp = angular.module('myApp',[]);

myApp.controller('storeController', ['$scope', '$http', function($scope, $http) {

    $scope.store = {products: getData()};

    function getData() {
        $http.get('products.php')
          .success(function(data) {
              return data;
        });
    }
}]);

Are you getting any errors in the console? 您在控制台中遇到任何错误吗?

I really don't know how your code works, but you said that this code is working fine: 我真的不知道您的代码如何工作,但是您说这段代码可以正常工作:

function store() {
    this.products = [
        { num: 1, code: 'TEST', category: 'Diverse', name: 'TEST', src: "test.png", description: 'info her', price: 2, cal: 10 }
    ];
}

And I'm assuming you have an access to the $http somehow. 我假设您可以通过某种方式访问$http

To convert the above code to use the $http , it should be like this instead: 要将上面的代码转换为使用$http ,应改为:

function store() {
    var self = this;

    $http.get('products.php')
        .success(function(data) {
            self.products = data;
        });
}

Hope this helps. 希望这可以帮助。

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

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