简体   繁体   English

如何从外部AngularJS访问函数

[英]How To Access Function From Outside AngularJS

I have absolutely never used AngularJS - I am able to handle all parts of someone else's script - but am lost with a newb idea of accessing the functions outside of the Class? 我绝对从未使用过AngularJS-我能够处理别人脚本的所有部分-但是迷失于访问Class之外的函数的新想法吗? Or whatever it is called 0_o - I have tried to look this up - but I just get lost. 或任何称为0_o的东西-我都尝试过查找-但是我迷路了。

The script Im working on is like this: 我正在处理的脚本是这样的:

app.controller("searchFormController", function($scope,$http){


    $scope.getSomeDetails = function(filter){

        alert("you are doing stuff in this function");

    }


})

The function is typically called from a form outside of the script - like this: 通常从脚本外部的表单中调用该函数-像这样:

<select id="transmissions" name="transmission" ng-model="transmission" ng-change="getSomeDetails(true)">

When the above form element is selected - the function getSomeDetails is called properly. 选择上述表单元素后,将正确调用函数getSomeDetails。

Now I simply want to call the same function with a text link instead. 现在,我只想使用文本链接来调用相同的函数。 I have tried: 我努力了:

<span onClick="getSomeDetails(true)">Get Details </span>

Not sure what I am doing wrong - 不知道我在做什么错-

I just want to call the same function but using a text link? 我只想调用相同的功能,但使用文本链接?

Thanks for your help. 谢谢你的帮助。

app.controller("searchFormController", function($scope,$http){
    $scope.getSomeDetails = function(filter){
        alert("you are doing stuff in this function");
    };
});

Here searchFormController is the your angular function.So attached this function to html template with ng-controller attribute. 这里的searchFormController是您的角度函数,因此将此函数附加到具有ng-controller属性的html模板。 functions and variable mentioned inside this searchFormController will be available to the template in which you have bind that function with ng-controller . searchFormController提到的函数和变量将可用于您已将该函数与ng-controller绑定的模板。

Your getSomeDetails function is in this searchFormController . getSomeDetails功能在此searchFormController So in html template where you wants to call this function before that you needs to bind the controller to it's parent element or to that element so that function scope is available where you needs to call. 因此,在要先调用此函数的html模板中,需要将控制器绑定到其父元素或该元素,以便在需要调用的函数作用域可用。

Here you have to call function on the click of link so use ng-click . 在这里,您必须在点击链接时调用函数,因此请使用ng-click and call the function. 并调用该函数。

Your span tag should be like this. 您的span标签应该是这样的。

<span ng-controller="searchFormController" ng-click="getSomeDetails(true)">Get Details </span>
<span ng-click="getSomeDetails(true)">Get Details</span>

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

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