How can i use javascript code inside angularjs

I need to run this code in angularjs.

When I try this function of the model it doesn't work.

It doesn't even show a warning or console.

So what is the way to use these 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>

      

+3


source to share


5 answers


If I understand your requirement correctly,

You need to execute a separate java-script function.

For angular app, this is not the correct way to run javascript from angular scope.

any, if this is absolutely true, you can try replacing ng-click="name()"

withonclick="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>
      

Run codeHide result




@Update

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

There are several advantages of using the following statements: -

  • It will add the proprely injection dependency which is the basic concept behind angularjs

  • It will ensure that the external function exists before the application starts.

  • This will add more flexibility and control to the java script function in angular.

  • The snippet below will remove external access to the function and protect your application.
  • This is the best way to add external libraries such as 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;
  }
  myFunction.$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>
      

Run codeHide result


+5


source


your code should be like this.



<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    <script >
      var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name=function(){
alert("text");
}
});

    </script>
  </head>

  <body ng-controller="MainCtrl">
    <div>
{{sample}}
</div>
<div><button ng-click="name()">click me</div>
  </body>

</html>
      

Run codeHide result


First you need to tell angular js cdn and then create module and controller as above

+2


source


Here's an example of a controller.

<body ng-app="myApp" ng-controller="myCtrl">
  <div>
    <button ng-click="name()">
  </div>
  <script>

    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
        $scope.name = name;
        function name(){
          alert("text");
        }
    });
  </script>
</body>

      

+2


source


You can use directive, here is an example:

// in your JS source

angular.directive('something', [function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            element.bind('click', function() {
                alert('yup');
            });
        }
    };
}]);

      

// in your HTML file

<button type="button" something>Click Me!</button>

      

This allows you to easily use different code snippets / functions for the whole project.

Another solution

<!DOCTYPE html>
    <html>
    
    <head>
      <title>example</title>
      <script data-require="jquery@*" data-semver="3.1.1" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
    </head>
    
    <body ng-app="app" ng-controller="exampleController as ctrl">
      <button ng-click="ctrl.name()">click me to angular</button>
      <button ng-click="name()" class="btn">click me to javascript</button>
      <script>
        var app = angular.module("app", []);
        app.controller('exampleController', [function() {
          this.name = function() {
            alert("angular text");
          }
        }]);
    
        //JQuery Lib must be included your project 
        $(document).on('click', '.btn', function() {
          eval($(this).attr('ng-click'));
        });
    
        function name() {
          alert("javascript text");
        }
      </script>
    </body>
    
    </html>
      

Run codeHide result


+2


source


You should be able to run any javascript inside angular controller inside script tag in html file. Your html file should look something like this:

<!DOCTYPE html>
<html>
<head>
    <title>example</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
</head>
<body ng-app="app" ng-controller="exampleController as ctrl">
        <button ng-click="ctrl.name()">click me</button>
</body>
</html>


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

app.controller('exampleController', [function() {
    this.name = function(){
        alert("text");
    }
}]);
</script>

      

0


source







All Articles