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

2
  • can you pls elaborate pls add html and js code in a working format Commented Mar 14, 2017 at 11:39
  • See This: stackoverflow.com/questions/16709373/… Commented Mar 14, 2017 at 11:39

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

you're mixing vanilla js and angularjs. And you're manipulating DOM from controller
@Gonzalo.- no no its just to access controller method from out side of it.

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.