Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I have a plain javascript function display(str) which I want to call inside ng-if. This function is outside the scope of controller

HTML

<body ng-app="myApp">
   <div id="mycntrl" ng-controller="mainController">
      <select id="selectid1" name="selectid1" ng-model="angularselect">
        <option ng-value=""></option>
        <option ng-value="val1">Value 1</option>
        <option ng-value="val2">Value 2</option>
      </select> Value : {{angularselect}}
   <div ng-if="display(angularselect) === true">
    <p>
       Returned true
    </p>
   </div>
  </div>
</body>

JS

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

myApp.controller('mainController',function($scope){

});

function display(str)
{
    console.log('Javascript function called with argument '+str);
    return true;
}

FIDDLE

share|improve this question

You can make global function in your app.run module like this.

    myApp.run(function($rootScope){
       $rootScope.display = function(str) {
          console.log('Javascript function called with argument '+str);
          return true;
        }
    })

and use it in html code like this:

<div ng-if="$root.display(angularselect) === true">

here is the fiddle JsFiddle

share|improve this answer

You can make use of $window object to access global variable in angular application.

Reference: https://docs.angularjs.org/api/ng/service/$window

Fiddle: https://jsfiddle.net/7kLr89cc/4/

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

myApp.controller('mainController',function($scope,$window){
    $scope.displayHere = function(str){
      return $window.display(str);
    };
});

function display(str)
{
    console.log('Javascript function called with argument '+str);
    return str;
}
share|improve this answer

You can assign display method using scope if they both are in same file.

Example -

myApp.controller('mainController',function($scope){
$scope.display = display;
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.