Skip to content Skip to sidebar Skip to footer

Variable Undefined In Service, After Saving A Value In It (angularjs)

I'm writing a phone directory app with Cordova and thus using AngularJs for the logic. I'm using this service to connect to the server (its a Diffusion Push server) and sending/rec

Solution 1:

First thing I see is that var employees = []; is not bound to anything. So how could you expect to access it via EmployeeService.employees?

Second thing is that your service does not return anything. If you look at the documentation you can see the service that is injected to your controller takes the value of what is returned from the service.

This is generally how you would build a service:

angular.module('app', []).service('helloService', function() {
  varService = function() { /* INIT CODE HERE */ };

  Service.prototype.sayHello = function() {
    console.log('hello, world;')
  };

  returnnewService();
})

You could also return an object literal, a string, a function, pretty much anything but the point is that you return something. You're just binding methods it to this.

Third thing is that EmployeeService.initConnection() is an asynchronous call there's no guarantee that the connection is initialized by the time you call sendMessage(). This is OK but you need to make sure the connection is ready by the time you trigger either of the service methods. You can do this by blocking interaction until a promise is resolved.

// controller template scope
<button ng-click="sendMessage()" ng-disabled="connecting">Send Message</button>

// controller$scope.connecting = true;
EmployeeService.initConnection().then(function() {
   $scope.connecting = false;
});

// service
.service('EmployeeService', ['$q', function() {
  var clientSession;
  var employees = [];
  var service = {}

  service.initConnection = function() {
    var defer = $q.defer();

    // do this in connection success callback
    defer.resolve();

    // do this if the connection fails
    dewfer.reject();

    return defer.promise;  
  }

  return service;
}]);

That's about all the advice I can give until you are more specific.


Edit: You can watch your service for updates like so:

$scope.watch(
  function() { return service.currentResult() },
  function(employees) {
    $scope.employees = employees;
  }
);

This will always keep $scope.employees up to date every digest cycle.

Solution 2:

I think that you need to put the employee as a specific index of your array

stream.on('message', function(message, id) {
  //Called when a message is recieved
  employees[id] = JSON.parse(message.content);
  console.log(employees);
});

Thats if you have a load of employees that you add one at a time. But maybe I'm misunderstanding your code.

Solution 3:

$scope.employees is undefined because EmployeeService.employees is undefined.

In EmployeeService your should declare it as follow this.employees=[ ] so that you can access it globally.

Now for undefined case in clearResult function, add some dummy value to variable this.employees and try to access it clearResult function.

Use breakpoints to get step by step code checking.

I hope this will help you. Thanks

Post a Comment for "Variable Undefined In Service, After Saving A Value In It (angularjs)"