1

I have a submitDetails function inside controller in app.js file and I want to call it from a javascript function. But its throwing error Error: Submit Details is undefined

clickApply: function(e) {
            
            console.log("hi&hello");
            
                    angular.element('#span').scope().submitDetails();
               
            this.hide();
            this.element.trigger('apply.daterangepicker', this);

        }

$rootScope.submitDetails=function()
Saurav Dutta
  • 83
  • 2
  • 10
  • can you pls elaborate pls add html and js code in a working format – SAMUEL Mar 14 '17 at 11:39
  • See This: http://stackoverflow.com/questions/16709373/angularjs-how-to-call-controller-function-from-outside-of-controller-component – Jenny Mar 14 '17 at 11:39

1 Answers1

1

This can be done as below

JS

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

  app.controller('ctrl', function($scope) {
    $scope.sayHello = function() {
      $scope.msg = 'Hello';
    }
  });
  setTimeout(function() {
    var scope = angular.element(document.getElementById("btn")).scope();
    scope.$apply(function() {
      scope.sayHello();
    });
  }, 2000)

HTML

  <div ng-app='myApp'>
    <div ng-controller='ctrl' id='ctrl'>
      {{msg}}
      <button id='btn' ng-click='sayHello()'>
        Go
      </button>
    </div>

  </div>

Hope this will help you Jsfiddle link

Jayant Patil
  • 1,537
  • 2
  • 11
  • 18