5

I tried to pass value form one module to another module in angularjs. using .value which is working fine.

working:-

var app = angular.module('app', []);
app.value('movieTitle', 'The Matrix');
var app1 =angular.module('app1', ['app']);
app1.controller('MyController', function (movieTitle) {
//Here I am getting value. which is working fine.
console.log(movieTitle)
})

Not working:-

var app = angular.module('app', []);
app.value('movieTitle', 'The Matrix');
app.controller('MyController', function (movieTitle) {
//Here I override the value.
movieTitle = "The Matrix Reloaded";
})
var app1 =angular.module('app1', ['app']);
app1.controller('MyController', function (movieTitle) {
//Here I am getting old value not update value.
console.log(movieTitle)
})

In Second sample I tried to update the value its updating fine. but while Am access the value from other module that time it shows only old value not updated one can anyone help me this. where i did mistake...

1
  • got a chance to look at the answers? Commented Jul 21, 2016 at 8:22

2 Answers 2

1

JavaScript strings are immutable, so you cannot update the injected value (as it's a string) - you're just changing the content of the injected variable. You could take another approach at containing the string within an object, now you can update the string in the object:

var movie = { title: 'The Matrix' };

angular.module('app', [])
    .value('movie', movie)
    .controller('MyController', function (movie) {
        //Here I override the value.
        movie.title = "The Matrix Reloaded";
    });

angular.module('app1', ['app'])
    .controller('MyController', function (movie) {
        console.log(movie.title);
    });
Sign up to request clarification or add additional context in comments.

Comments

0

The value is basically a string that is its is a primitive type and have no reference so the value is binded once.

I've made a fiddle with a factory and used it for inter module application as your requirement

var app = angular.module('myApp', []);
var app1 = angular.module('myApp1', ['myApp']);

app.controller('HelloCtrl', HelloCtrl);
app.controller('GoodbyeCtrl', GoodbyeCtrl);
app1.controller('ctrl2', ctrl2);
app.factory('testFactory', function(){
        var _name = 'hello';
    return {
        getName: function(text){
            return _name;
        },
        setName: function(name){
            _name = name;
        }  
    }               
});

function HelloCtrl($scope, testFactory){
    $scope.name = testFactory.getName();
    testFactory.setName('hello2');
}

function GoodbyeCtrl($scope, testFactory){
    $scope.name = testFactory.getName();
    testFactory.setName('hello3');
}

function ctrl2($scope, testFactory){
    $scope.name = testFactory.getName();
}

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.