1

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

3 Answers 3

1

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

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

Comments

0

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;
}

Comments

0

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

Example -

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.