简体   繁体   English

从jQuery / JavaScript角度调用函数-属性未定义错误

[英]Angular calling a function from jquery/javascript - property undefined error

I'm trying to call an angular controller from my JavaScript. 我正在尝试从JavaScript调用角度控制器。 This is the first time I've used Angular for anything and I'm getting a bit lost! 这是我第一次使用Angular进行任何操作,但是我有点迷路了!

I've followed this example: 我遵循以下示例:

http://dotnet-concept.com/Tips/2015/6/5798829/How-to-call-angularJS-function-from-javascript-jquery http://dotnet-concept.com/Tips/2015/6/5798829/How-to-call-angularJS-function-from-javascript-jquery

But I'm getting this error: 但我收到此错误:

Uncaught TypeError: Cannot read property 'TestAngularMethod' of undefined(…) 未捕获的TypeError:无法读取undefined(…)的属性'TestAngularMethod'

Initially I had my own code in there, but I've stripped it all out to try and get this example to work first, but I'm not having any luck. 最初,我在其中拥有自己的代码,但是我已经剥离了所有代码以尝试使该示例首先生效,但是我没有任何运气。

Can anyone see where I'm going wrong? 谁能看到我要去哪里错了?

HTML 的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>

ANGULAR 角度

   var myApp = angular.module('myApp', []);

    myApp.controller('TestController', ["$scope", function ($scope) {
        $scope.TestAngularMethod = function () {
            alert('Hello you are calling angular js method.');
        }
    }]);

JavaScript 的JavaScript

angular.element(document.getElementById('divID')).scope().TestAngularMethod();

I've looked at these without any luck at figuring out my own issue: 我看了这些都没有发现自己的问题的运气:

Call angularjs function using jquery/javascript 使用jquery / javascript调用angularjs函数

AngularJS. AngularJS。 How to call controller function from outside of controller component 如何从控制器组件外部调用控制器功能

Call Angular Function with Jquery 用jQuery调用Angular函数

Thanks! 谢谢!

Despite @Fissio is right ... you can try this code: 尽管@Fissio是正确的...您可以尝试以下代码:

EDITED 已编辑

JAVASCRIPT 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 /*
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM