Skip to main content

A wee bit ugly, but I've added registration of scope variables to my service for a toggle:

myApp.service('myService', function() {
    var self = this;
    self.value = false;
    self.c2 = function(){};
    self.callback = function(){
        self.value = !self.value; 
       self.c2();
    }; 

    self.on = function(){
        return self.value;
    }; 

    self.register = function(obj, key){ 
        self.c2 = function(){
            obj[key] = self.value; 
            obj.$apply();
        } 
    }; 

    return this; 
});

And then in the controller:

function MyCtrl($scope, myService) {
    $scope.name = 'Superhero';
    $scope.myVar = false;
    myService.register($scope, 'myVar'); 
}

A wee bit ugly, but I've added registration of scope variables to my service for a toggle:

myApp.service('myService', function() {
var self = this;
self.value = false;
self.c2 = function(){};
self.callback = function(){
    self.value = !self.value; 
    self.c2();
};
self.on = function(){return self.value;};
self.register = function(obj, key){ 
    self.c2 = function(){
        obj[key] = self.value; 
        obj.$apply();
    } 
};
return this;});

And then in the controller:

function MyCtrl($scope, myService) {
$scope.name = 'Superhero';
$scope.myVar = false;
myService.register($scope, 'myVar');}

A wee bit ugly, but I've added registration of scope variables to my service for a toggle:

myApp.service('myService', function() {
    var self = this;
    self.value = false;
    self.c2 = function(){};
    self.callback = function(){
        self.value = !self.value; 
       self.c2();
    }; 

    self.on = function(){
        return self.value;
    }; 

    self.register = function(obj, key){ 
        self.c2 = function(){
            obj[key] = self.value; 
            obj.$apply();
        } 
    }; 

    return this; 
});

And then in the controller:

function MyCtrl($scope, myService) {
    $scope.name = 'Superhero';
    $scope.myVar = false;
    myService.register($scope, 'myVar'); 
}
Source Link
nclu
  • 1.1k
  • 1
  • 9
  • 19

A wee bit ugly, but I've added registration of scope variables to my service for a toggle:

myApp.service('myService', function() {
var self = this;
self.value = false;
self.c2 = function(){};
self.callback = function(){
    self.value = !self.value; 
    self.c2();
};
self.on = function(){return self.value;};
self.register = function(obj, key){ 
    self.c2 = function(){
        obj[key] = self.value; 
        obj.$apply();
    } 
};
return this;});

And then in the controller:

function MyCtrl($scope, myService) {
$scope.name = 'Superhero';
$scope.myVar = false;
myService.register($scope, 'myVar');}