0

I have main controller ..In that I am using another controller.In the 2nd controller I am again using another controller.How to get a connection between first controller and 3rd controller in angularjs .!st is the main controller .And 2nd is for pop up on page load .And 3rd is for the control action of the pop up.CAn anyone solve this problem.

//mainform.js

var app=angular.module("myapp",['ngRoute','ngDialog','ngSanitize','addfrmapp','homeapp']);

app.controller("controller", ["$scope","ngDialog",function($scope,ngDialog){

    $scope.getname=function(){

        $scope.value=$("#fmname").val();

    };

    $scope.setname=function(){
        $("#fmname").val("");
    };

    $scope.inputhtml=function(){
    console.log("hi");
   };
}]);
app.config(function($routeProvider){
    $routeProvider.when("/Home",
            {
                templateUrl:"Htmlfiles/Home.html"

            })
        .when("/Add",
                {
                    templateUrl:"Htmlfiles/AddForm.html",
                    controller:"addform"
                })
        .when("/View",
                {
                    templateUrl:"Htmlfiles/ViewForm.html"
                });
        $routeProvider.otherwise({ 
            templateUrl:"Htmlfiles/Home.html"
        });
});

//addform.js

var app=angular.module("addfrmapp",['ngDialog']);
app.controller("addform",['$scope','ngDialog',function($scope,ngDialog){

    $scope.$parent.value="";
    ngDialog.open({

        template: 'Htmlfiles/formname.html',
        closeByEscape:false,
        closeByDocument:false,
        scope: $scope,
       showClose:false


    });
    $scope.savebtn=function(){

        if($("#fmname").val()=="")
            {
                alert("Form name must be given in the input box");
            }
        else{
            $scope.$parent.getname();
            ngDialog.closeAll();
        }
    };
    $scope.cancelbtn=function(){

                $scope.$parent.setname();

    };

    $scope.addtext=function(){


                ngDialog.open({
                    template:"popupfiles/textpopup.html",
                    closeByDocument:false,
                    closeByEscape:false,

                    controller:['$scope',function($scope){
                        $scope.expndtxt=function(){

                            $scope.$parent.inputhtml();
                        };
                    }]

                });
            };  
            $scope.addradio=function(){

                ngDialog.open({
                    template:"popupfiles/radiopopup.html",
                    closeByDocument:false,
                    closeByEscape:false,
                });

            };
            $scope.addcheckbox=function(){

                ngDialog.open({
                    template:"popupfiles/checkboxes.html",
                    closeByDocument:false,
                    closeByEscape:false,

                });

            };
            $scope.addsubmitbutton=function(){

                ngDialog.open({
                    template:"popupfiles/submitbuttons.html",
                    closeByDocument:false,
                    closeByEscape:false,

                });

            };


}]);

Now when i click on a button which goes to the function expndtext it going to the function but its not calling inputhtml() function which is in main js file.

Moppo
  • 18,797
  • 5
  • 65
  • 64
Vindya Veer
  • 139
  • 1
  • 3
  • 15

4 Answers4

0

Generally, angularjs services are used for implementing common functions and communication between different controllers. Read them up here.

Tarun Dugar
  • 8,921
  • 8
  • 42
  • 79
0

You can use $rootScope, it will be available estou all controllers.

You can also create function and the controllers accessing it. https://docs.angularjs.org/api/ng/service/$rootScope

Alvaro Silvino
  • 9,441
  • 12
  • 52
  • 80
0

The best way to communicate between controllers is to create a common service and use it by injecting it in the controllers. It is preferred when there is no parent-child relationship between the controllers, you can use services as there is no parent-child relationship in your case.

The other way when there exists such relationship, then it works much more like inheritence, you can access parent objects and methods from the child controllers directly without using any services.

Ritt
  • 3,181
  • 3
  • 22
  • 51
0

You can try to broadcast an event in parent controller with data that you need to send to the other controller, and in second controller listen for that event and use the data from it. It can be done like this:

in first controller:

$rootScope.$broadcast('mydatachanged', $scope.myData);

in second controller:

$scope.$on('mydatachanged', function(event, myData) {
// do what you want to do with changed data
});
Diana R
  • 1,174
  • 1
  • 9
  • 23