2017-02-10 107 views
2

我想從JavaScript如何從javascript調用angularjs函數?

的JavaScript調用我的AngularJs功能

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> 
+4

這不是*角*的方式做這 – Weedoze

+0

//angular.element(「#內容」)的範圍( ).getorganisationUnits(); – Laurianti

+1

@Laurianti這不是如何在AngularJS中工作。請爲AngularJS提供正確的解決方案 – Weedoze

回答

3

這是如何做到這一點在角方式

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>

+0

我會在這裏添加一個ng-change – Groben

+0

@Groben Op的似乎只在點擊按鈕時才加載 – Weedoze

+0

是的,你說的對,我的不好。得到我的投票。 – Groben