How to update value inside angularjs controller?

I tried passing the form value to one module to another module in angularjs. using a value that works 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)
})

Does not work: -

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 the second example, I tried to update the value corresponding to its updating. but while I access the value from another module, at this time it shows only the old value, not updated, someone can help me. where am I wrong ...

+4
source share
2 answers

JavaScript , ( ) - . , :

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);
    });
+1

string, , .

fiddle factory inter module

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();
}

, .

0

All Articles