Sudiksha Nagvanshi
Sudiksha Nagvanshi

Reputation: 51

How do i call angularjs function from javascript?

I want to call my AngularJs function from JavaScript

JavaScript

function loadData1() {
    var dropdown = document.getElementById("dropdown");
    var a = dropdown.options[dropdown.selectedIndex].value;
    if (a=="organisationUnits") {
        getorganisationUnits();
    }
}

AngularJs

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
    //angular.element('#myCtrl').scope().getorganisationUnits();
    $scope.getorganisationUnits = function () {
        window.alert("Show data");
    }
});

HTML

<div ng-app="myApp" ng-controller="myCtrl" id="content">
    <select id="dropdown">
        <option value="selected"> Please Select Option</option>
        <option value="organisationUnits"> Organisation Units</option>
    </select>
    <input type="button" value="Go" id="getall_go" onclick="loadData1()"></input>
</div>

Upvotes: 2

Views: 2216

Answers (1)

Weedoze
Weedoze

Reputation: 13953

This is how to do it in an angular way

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.load = function() {
    console.log("Dropdown value : " + $scope.drop);
    if ($scope.drop === "organisationUnits")
      console.log("Show data");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl" id="content">
  <select id="dropdown" ng-model="drop">
    <option value="selected">Please Select Option</option>
    <option value="organisationUnits">Organisation Units</option>
  </select>

  <input type="button" value="Go" id="getall_go" ng-click="load()" />
</div>

Upvotes: 3

Related Questions