Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use javascript code inside angularjs?

I need to run this code in angularjs.

When I try to execute this model function it is not running.

It is not even showing alert or console.

So what is the way to use this script files in angularjs

Here is my code in example.html:

<div>
{{sample}}
</div>
<div><button ng-click="name()"></div>
<script>
function name(){
alert("text");
}
</script>
like image 508
Deepak Pookkote Avatar asked Apr 24 '17 04:04

Deepak Pookkote


People also ask

Can I use JavaScript in AngularJS?

You need to execute a separate java-script function. For an angular application it is not a proper way to run javascript out of scope of angular. It will ensure that the external function exist before starting app. It will add more flexibility and control over the java script function in angular.


1 Answers

If i understand your requirement correctly,

You need to execute a separate java-script function.

For an angular application it is not a proper way to run javascript out of scope of angular.

any if it is absolutely requried you can try replacing ng-click="name()" with onclick="name()"

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

app.controller('exampleController', [function() {
  this.myFunction = function() {
    alert("Iam from angular controller");
  }
}]);

function myFunction() {
  alert("Iam from javascript");
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

<div ng-app="app" ng-controller="exampleController as ctrl">
  <button ng-click="ctrl.myFunction()">click me</button>
  <button onclick="myFunction()">click me</button>
</div>

@Update

If you want to use a java script library or function or constants in angularjs the my suggested way is to add a factory or service that ensure the library or function or constants but it is not simple as above solution.Here iam adding a snippet based on above solution

There are some advantages of using following approch :-

  1. It will add dependency injection proprely which is a basic concept behind angularjs

  2. It will ensure that the external function exist before starting app.

  3. It will add more flexibility and control over the java script function in angular.

  4. The below snippet will remove the external access of function and secure your application
  5. This is the best way in which you can add external libraries like jquery loadash or any js libraries to your code

(function() {
  function exampleController(myFunction) {
    this.myFunction = function() {
      alert("Iam from angular controller");
    }
    this.externalJSFunction = myFunction
  };
  exampleController.$inject = ['myFunction'];
  
  function myFunctionFactory($window) {
    if (!$window.myFunction) {
      throw new Error("myFunction is not defined");
    }
    this.myFunction = $window.myFunction;
    /* You can add 
     $window.myFunction = undefined; 
     if the function ($window.myFunction) is not required by 
     some other library or function in window scope  
     in such cases it gives added security by 
     restricting access of this from window scope and dis allows modification
     */
    return this.myFunction;
  }
  myFunctionFactory.$inject = ['$window'];
  
  var app = angular.module("app", []);
  app.controller('exampleController', exampleController);
  app.factory('myFunction', myFunctionFactory);

})();
function myFunction() {
  alert("Iam from javascript");
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<div ng-app="app" ng-controller="exampleController as ctrl">
  <button ng-click="ctrl.myFunction()">click me For angular Function</button>
  <br/>
  <button ng-click="ctrl.externalJSFunction()">click me For external JS Function </button>
  <br/>
  <button onclick="myFunction()">click me for checking exteranl acess </button>
</div>
like image 88
SAMUEL Avatar answered Sep 19 '22 13:09

SAMUEL