dougudu3564 2014-08-14 14:54
浏览 48
已采纳

AngularJS $ http.get产品

i created my new store in angularjs. In there i have a function called store, where it gets all my products. In the start i just wrote all the products in there, but now i want to get them from a mysql database. I think that the problem is something in the $http.get, but dont know what.

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:

function store() {

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

}

My products.php file echo this and it does encode to json:

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

products.php file:

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

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

app.js

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

    // create store
    var myStore = new store();
});
  • 写回答

2条回答 默认 最新

  • dongxingji3882 2014-08-14 17:03
    关注

    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.

    To convert the above code to use the $http, it should be like this instead:

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

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装