Link to home
Start Free TrialLog in
Avatar of Dinesh Kumar
Dinesh KumarFlag for India

asked on

Angular Question

Experts!
 
I am learning Angular and trying to get products from the database, the products in the following link are coming as static from store.js file.
 
http://www.codeproject.com/Articles/576246/A-Shopping-Cart-Application-Built-with-AngularJS

What is the way of doing it.  
 
I am trying the following code, but I don't see any error or any success. Could you please help  me.
(
function () {
var app = angular.module("productViewer", []);
var ProductController = function ($scope, $http) {
var onUserComplete = function (response) {
alert(response.data);
$scope.products = response.data;
};
var onError = function (reason) {
alert("error");
$scope.error = "Could not fetch the Products";
};
 
$http.get("https://api.github.com/users/robconery")
 .then(onUserComplete, onError);
};
app.controller(
"ProductController", ["$scope", "$http", ProductController]);
}());

Thanks
Dinesh Kumar


Please Note: I wanted to make change in the existing code given on the code project article mentioned earlier.
SOLUTION
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dinesh Kumar

ASKER

Let me try this until then I have created a ProductService.js having the following code:

(function () {

    var products = function ($http) {

        var getProducts = function (username) {
            return $http.get("http://localhost:60128/Products-Service/Products")
                        .then(function (response) {
                            alert(angular.toJson(response.data.value));
                            return angular.toJson(response.data.value);
                        });
        };

        return {
            getAllProducts: getProducts
        };

    };

    var module = angular.module("AngularStore");  //refering to Existing Module
    module.factory("ProductService", products);

}());

Open in new window


And in the store.js file, I am trying further code with some modification:

function store(ProductService) {
   
    this.products = ProductService.getAllProducts;
    alert(this.products);

but it says getAllProducts is undefined. :(
you are still writing function expressions. why?

why are you rewriting the tutorial?

If you want to learn angular, you have to learn the fundamentals of javascript first.
function declarations vs function expressions is just one of those fundamentals.
Thank you Kyle!