Updation of a shared object that is created inside factory method of a module is not updating in another module in Angular JS

36 views Asked by At

Hello I am a beginner in Angular JS and now I am stuck into something. I have made two modules

Menu and Canvas

Now I have declared a object inside the factory method of Menu module to make it available from Canvas module.

My code is as follow;

var Menu=angular.module("menu",[]);

Menu.factory("Data",['$rootScope', function($rootScope){

    var obj={};
    obj.server="http://localhost:8080";
    obj.transformations=null;
    obj.actions=null;
    obj.sources=null;
    obj.typeSelected=null;

    obj.save = function(data) {
         $rootScope.$broadcast('data:updated',data);
       }

    return obj;
}]);

Now I have a controller that update the values of obj's attributes.

Menu.controller("menuClick",function($scope,Data){

    $scope.obj=Data;

    $scope.clickOnMenu=function(type)
    {
        if(type.indexOf("Source")!=-1)$scope.obj.typeSelected="source";
        else if(type.indexOf("Transformation")!=-1)$scope.obj.typeSelected="transformation";
        else if(type.indexOf("Action")!=-1)$scope.obj.typeSelected="action";

        console.log($scope.obj);

        //added bootstrap here
        angular.bootstrap(document.getElementById("canvascontainer"), ['canvas']);
    }
})

Now when I am creating the Canvas module I am adding dependency of Menu module

var Canvas=angular.module('canvas',['menu']);

Canvas.controller("canvasOperation",["Data",'$scope',function(Data,$scope){

    $scope.obj=Data;

    $scope.clickCanvas=function()
    {
        console.log($scope.obj)
        if($scope.obj.typeSelected!=null)
        {
         console.log("fun started");
        }
    }
}])
 //removed it from here
//angular.bootstrap(document.getElementById("canvascontainer"), ['canvas']);

Now the problem is even after I update the typeSelected value from clickOnMenu method of Menu module the value of typeSelected is not updating in Canvas module and in Canvas module;

console.log($scope.obj.typeSelected)

giving output null.

I tried to explain every details. Can you please tell me where am I doing wrong?

0

There are 0 answers