2

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>
3
  • 4
    This is not the angular way of doing this Commented Feb 10, 2017 at 10:27
  • //angular.element('#content').scope().getorganisationUnits(); Commented Feb 10, 2017 at 10:30
  • 1
    @Laurianti This is not how to work in AngularJS. Please provide correct solution for AngularJS Commented Feb 10, 2017 at 10:35

1 Answer 1

3

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>

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

2 Comments

I would add a ng-change here instead
@Groben Op's seems to load only when the button is clicked

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.