factory http get explanation

24 views
Skip to first unread message

Thế Bảo Đinh

unread,
Sep 21, 2015, 10:39:56 PM9/21/15
to AngularJS
Hi all,
I have a code: 
app.factory("serviceDashboard", ['$http', function ($http) {
    var obj = {};

    obj.getMessages = function (username) {
        $http.get(serviceBase + 'Dashboard/GetMessages/', {
            params: { username: username, }
        }).then(function (data) {
            return data;
        });
    }
    return obj;
}])

app.controller('dashboard', function ($scope, $rootScope, serviceDepartment, $http, serviceDashboard) {
    $scope.loadMessages = function () {
        var username = $rootScope.globals.currentUser.username;
        serviceDashboard.getMessages(username).then(function (data) {
            $scope.messages = data.data;
        });
    }

    $scope.loadMessages();
});

When i run it will display error "cannot read property 'then' of undefined".
But when i change code in factory to :
  obj.getMessages = function (username) {
       return $http.get(serviceBase + 'Dashboard/GetMessages/', {
            params: { username: username, }
        });
    }

It works. I am newbie. Pls explain for me. Thanks.

Hai Huynh

unread,
Sep 22, 2015, 12:59:15 AM9/22/15
to AngularJS
Maybe help you
  
// Update or add new one  customer
    $scope.PostCustomer = function () {
        $.post("api/customerapi",
                     $("#cusForm").serialize(),
                     function (value) {

                         // Refresh list
                         $http.get("/api/customerapi")
                         .success(function (response) {
                             $scope.Customer = response
                         });

                         alert("Saved successfully.");
                     },
                     "json"
               );
    }

    // Delete one customer based on id.
    $scope.delCustomer = function (id) {
        if (confirm("Are you sure you want to delete this customer?")) {
            // todo code for deletion
            $http.delete("api/customerapi/" + id).success(function () {
                alert("Deleted successfully.");
                // Refresh list
                $http.get("/api/customerapi")
                .success(function (response) {
                    $scope.Customer = response
                });

            }).error(function () {
                alert("Error.");
            });
        }
    };


Bao

unread,
Sep 22, 2015, 1:09:59 AM9/22/15
to ang...@googlegroups.com
Thanks. But I need someone explain my code .

From: Hai Huynh
Sent: ‎9/‎22/‎2015 12:59 AM
To: AngularJS
Subject: [AngularJS] Re: factory http get explanation

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/9ts4H7IlN_E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages