使用jQuery / javascript调用angularjs功能 [英] Call angularjs function using jquery/javascript

查看:617
本文介绍了使用jQuery / javascript调用angularjs功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用javascript / jQuery的调用函数的角度,以下是我发现的链接

I am trying to call angular function using javascript/jQuery, following are the link I found

  • Call Angular Function with Jquery
  • Call controller function from outside of controller component

我已经在浏览器控制台相同的步骤,但得到的错误

I have taken the same steps but getting error in browser console

Uncaught TypeError: Cannot call method 'myfunction' of undefined

我创建了小提琴。如何调用myfunction的函数,并显示警报?任何想法?

I have created the fiddle. How to call myfunction function and alert is displayed? Any Idea?

推荐答案

解决方案中,您链接是正确的问题提供。的与您的实现问题是,你有没有指定的 ID 元素的正确。

Solution provide in the questions which you linked is correct. Problem with your implementation is that You have not specified the ID of element correctly.

其次,你需要使用负荷事件来执行你的code。目前,DOM没有加载元素,因此没有发现这样你得到错误。

Secondly you need to use load event to execute your code. Currently DOM is not loaded hence element is not found thus you are getting error.

HTML

<div id="YourElementId" ng-app='MyModule' ng-controller="MyController">
    Hi
</div>

JS code

angular.module('MyModule', [])
    .controller('MyController', function ($scope) {
    $scope.myfunction = function (data) {
        alert("---" + data);
    };
});

window.onload = function () {
    angular.element(document.getElementById('YourElementId')).scope().myfunction('test');
}

DEMO

这篇关于使用jQuery / javascript调用angularjs功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆