0

Thanks for all your input, but now I have more or less a similar problem, I have the data that needs to be stored in sql-server database, when I try to post it the data does not get written. Is my code structure correct?

self.CurrentDowntimeEvent = {
        method: 'POST'
        , url: 'someurl/test'
        , data: {
          DepartmentId: cookie
        , CategoryId: -1
        , Comment: ""
        , DowntimeStart: "2014-07-07T10:00:00"
        , DowntimeEnd: null
        }
        , headers: {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
    }).success(function (data) {
        console.log(data);
    }).error(function (data) {
    });
scniro
  • 16,844
  • 8
  • 62
  • 106
user3811714
  • 286
  • 2
  • 8
  • 21

1 Answers1

1

$http is a service which should be injected into the controller, so you shouldn't need self. to reference it:

self.RecordsSave = function () {
    for (var i = 0; i < self.Employees.length; i++) {
        var employee = self.Employees[i];
        var params = {
            CompanyNumber: employee.ClockNumber,
            Department: employee.DepartmentID,
            Present: employee.Present,
            Reason: employee.AbsentCode
        };
        $http.post(SAVE_EMPLOYEERECORDS, {
            params: params
        }).success(function (data) {
            alert("testing");

        });
    }
};
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
  • what is self? why it is used? – micronyks Jul 08 '14 at 07:41
  • Generally `self` is used as a reference to a parent object, so you know what object it references instead of depending on `this` e.g. `(function() { var self = this; var internalFunction = function() { self.hello(); }; }());` - I'm assuming that's what it is in this case – CodingIntrigue Jul 08 '14 at 07:42
  • But how would self.RecordsSave get called? – micronyks Jul 08 '14 at 07:49
  • @micronyks Could be anything - inside a controller, service, or something else. Hard to tell without more context – CodingIntrigue Jul 08 '14 at 07:56
  • @user, @ RGraham .How can you call RecordsSave() without using '$scope'. shouldn't it be $scope.RecordsSave=function()? – micronyks Jul 08 '14 at 08:07
  • @micronyks Firstly, you don't *need* to use scope, [you can use this](http://stackoverflow.com/questions/11605917/this-vs-scope-in-angularjs-controllers). Second, you're assuming this is a Controller. Could be a factory, module or directive link? – CodingIntrigue Jul 08 '14 at 08:09