我正在尝试从我的JavaScript调用角度控制器。这是我第一次使用Angular做任何事情而且我有点迷失了!
我遵循了这个例子:
http://dotnet-concept.com/Tips/2015/6/5798829/How-to-call-angularJS-function-from-javascript-jquery
但是我收到了这个错误:
Uncaught TypeError:无法读取属性'TestAngularMethod'的 未定义的(...)
最初我在那里有自己的代码,但是我已经完全剥离了它,试图让这个例子先工作,但我没有运气。
谁能看到我哪里出错了?
HTML
<div class="continueClass" id="divID" runat="server" ng-controller="TestController" ng-if="payment.type == 'PayPal' || payment.type == 'WorldPay'">
<asp:Button runat="server" Text="Continue" OnClick="Continue_Click"/>
</div>
角
var myApp = angular.module('myApp', []);
myApp.controller('TestController', ["$scope", function ($scope) {
$scope.TestAngularMethod = function () {
alert('Hello you are calling angular js method.');
}
}]);
的JavaScript
angular.element(document.getElementById('divID')).scope().TestAngularMethod();
我在查明自己的问题时毫不费力地看着这些:
Call angularjs function using jquery/javascript
AngularJS. How to call controller function from outside of controller component
Call Angular Function with Jquery
谢谢!
答案 0 :(得分:2)
尽管@Fissio是对的......你可以试试这段代码:
<强> EDITED 强>
JAVASCRIPT
var scope = angular.element('[id=divID]').scope();
if(scope ){ //check whether the div exist
scope.TestController.TestAngularMethod(); //should execute your method
}
else{
/* code if scope doesn't exist, div, probably removed because
payment.type == 'PayPal' || payment.type == 'WorldPay' is probably false /*
}