1

I am new to angular js. I have two ng-controller in two Html page. I want to share data from one controller into another controller. Here is the service i have created:

app.service('sharedProperties', function() {
    var stringValue = 'test string value';
    var objectValue = {
        data: 'test object value'
    };

    return {
        getString: function() {
            return stringValue;
        },
        setString: function(value) {
            stringValue = value;
        },
        getObject: function() {
            return objectValue;
        }
    }
});

Here is controller1:

 app.controller('FirstCtrl',function($scope,sharedProperties){
         sharedProperties.setString("Hi");
  });

Here is controller2:

app.controller('SeccondCtrl',function($scope,sharedProperties){
               window.alert(sharedProperties.getString());

});

Instead of getting alert containing string 'Hi' i am getting 'test string value'.

NB. i am using FirstCtrl and SeccondCtrl in two HTML file. And after setting string from FristCtrl i redirect to the second HTML file .

Rafi Ud Daula Refat
  • 2,187
  • 19
  • 28

3 Answers3

1

I've created jsfiddle and it works fine.

app.controller('FirstCtrl',function($scope,sharedProperties){
    $scope.set =  function(){
        sharedProperties.setString("Hi");
    }
  });

app.controller('SeccondCtrl',function($scope,sharedProperties){
    $scope.get = function(){
        window.alert(sharedProperties.getString());
    }

});
Oleksii
  • 328
  • 3
  • 17
  • It is not working when i am having the two controllers in different view (Html file). I am setting data from onek Html and want to show the data in another Html. – Rafi Ud Daula Refat Apr 22 '15 at 10:55
  • Understand your situation. It's a pity that jsFiddle doesn't allow create multiply files. Plunker can help us to figure out the problem.. – Oleksii Apr 22 '15 at 11:00
0

I don't understand why you set the methods under a return, why not try this approach on the service?. Either way you could set the service as a function under $scope ($scope.sharedProperties) in your main js, making it "globally" usable.

app.service('sharedProperties', function() {
    var stringValue = 'test string value';
    var objectValue = {
        data: 'test object value'
    };
        this.getString: function() {
                return stringValue;
        };
    this.setString: function(value) {
            stringValue = value;
        };
    this.getObject: function() {
            return objectValue;
       };
    return {
        getString: getString,
        setString: setString,
        getObject: getObject
    }
});
0

Service have this reference you write your service method as per below

app.service('sharedProperties', function() {
        var stringValue = 'test string value';
        var objectValue = {
            data: 'test object value'
        };

       this.getString: function() {
                return stringValue;
            }
       this.setString: function(value) {
                stringValue = value;
            }
        this.getObject: function() {
                return objectValue;
            }

    });
B.V.S Bharat Kumar
  • 202
  • 1
  • 5
  • 14