Angular Services

I am trying to use id between controllers in angular

I created the service as follows:

app.factory("idService", function() {
    var id;
    addId = function(id) {
        id = id;
    };
    getId = function() {
        return id;
    };    
});

In my controller, I am trying to use this service as follows:

app.controller('photoFormController', ['$scope', '$http', 'idService' , function($scope,   $http, idService) {

       $scope.id = idService.getId();
}]);

I get an error, cannot call the undefined method, it is obvious that I entered the service incorrectly. Can anyone help?

EDIT:

Based on the solution below, the service no longer generates errors, however, I can’t get the identifier back, I see that it is installed from one controller, however, when it is extracted, it remains undefined:

app.factory("idService", function() {
    var id;
    addId = function(id) {
        id = id;
        console.log("added id of: " + id);
    };
    getId = function() {
        console.log("trying to return : " + id);
        return id;
    };
    return {
        addId: addId,
        getId: getId
    };
});
+4
source share
5 answers

factory. :

app.factory("idService", function() {
    var _id; //use _id instead to avoid shadowing your variable with the same name id
    var addId = function(id) { //use var to avoid creating a property on the global object
        _id = id;
    }; 
    var getId = function() { //use var to avoid creating a property on the global object
        return _id;
    };    

    return {
        addId : addId ,
        getId : getId 
    };
});
+6

, .

fiddle

:

myApp.controller('ctrl1', function($scope, myservice) {
  $scope.updateId = function() {
    myservice.setId($scope.input)
  };
});


myApp.controller('ctrl2', function($scope, myservice) {
    $scope.data = myservice.getData();
});

myApp.service('myservice', function() {

  var myservice = this;

  var data = {};

  myservice.setId = function(newValue) {
    data.id = newValue;
  };

  myservice.getData = function() {
    return data;
  }

});

, , , , , .

+1

, value():

JavaScript

angular.module('app',[]).
  value('share', {id: 123}).
  run(['share', function(share) {
    console.log(share.id); // <= 123
    share.id = 345;
  }]).
  controller('appController', ['$scope', 'share', function($scope, share) {
    $scope.id = share.id; // <= 345
  }]);

Plunker: http://plnkr.co/edit/m55hmFgBvi1rwVsYMeKU?p=preview

0

, :

'use strict';

angular
    .module('app')
    .factory('youService', youService);

youService.$inject = ['$rootScope'];
function youService($rootScope) {
    var service = {};
    var questions = [];
    // Data
    function getData() {
        return questions;
    };
    function setData(newQuestion) {
        questions.push(newQuestion);
    };
    function resetData() {
        questions = {};
    };

    // Services
    service.getData = getData;
    service.setData = setData;
    service.resetData = resetData;
    return service;
};

:

angular.module('app')
.controller('yourController', yourController);
stepFourController.$inject = ['$scope', 'youService'];
function yourController($scope, youService){
    // Get saved data from service
    $scope.model = youService.getData();
    // Set data to service
    youService.setData($scope.model);
    // Reset data in service
    youService.resetData();
}; 
0

- :

var myApp = angular.module('myApp', []);
myApp.service('shareId', [function () {
    var shareIdService = this;
    shareIdService.id = '';
    shareIdService.setId = function (id) {
        shareIdService.id = id;
    }
    shareIdService.getId = function () {
        return shareIdService.id;
    }
}]);

factory

myApp.factory('shareId', [function () {
    var id = '';
    return {
        setId:function (id){
            id=id;
        },
        getId:function (){
            return id;
        }
    }
}])
0

Source: https://habr.com/ru/post/1536437/


All Articles