angularjs项目中的jQuery脚本 [英] Jquery scripts in angularjs project

查看:105
本文介绍了angularjs项目中的jQuery脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究使用yeoman角度生成器生成的angularjs项目.

I am working on angularjs project that I generated using yeoman angular generator.

我有一个jQuery函数,我需要在大多数html视图中使用. 可能的解决方案是将此功能添加到例如script.js文件中,并将该文件作为引用添加到需要此功能的html视图中.但是,我认为这种解决方案不是很好.

I have a jQuery function that I need to use in most of my html views. A possible solution is to add this function to a script.js file for example, and add this file as reference in the html views that require this function. However, I don't think this solution is good.

yeoman生成一个angularjs项目时,在其index.html文件中添加了一个部分

When yeoman generates an angularjs project, in its index.html file, it adds a section

<!-- build:js({.tmp,app}) scripts/scripts.js -->

在本节中,添加了不同的脚本文件,因为我认为在构建时,这些文件将被统一在一个scripts/scripts.js文件中.

within this section, the different script files are added since as I think, on build, those files will be unified in a single scripts/scripts.js file.

我试图通过将功能添加到脚本文件中来执行相同的操作,并将此脚本文件的引用添加到本节中.问题是,当我尝试从任何视图调用该函数时,该函数均无法正常工作.

I tried to do the same by adding the function to a script file, and added the reference of the this script file to this section. The problem is that the function is not working when I try to call it from any view.

我该怎么解决?

推荐答案

DOM操作是使用angularjs中的指令完成的,您需要检出以下文档:

DOM manipulation is done with directives in angularjs you will need to check out the docs: https://docs.angularjs.org/guide/directive

如果不操作DOM,则可以创建可以注入到任何控制器中的服务. 服务文档: https://docs.angularjs.org/guide/services

If you are not manipulating the DOM you can create a service that can be injected into any controller. Services documentation: https://docs.angularjs.org/guide/services

从那里您可以将控制器中的功能分配给可以在视图中访问的范围.例如

from there you can assign a function in the controller to the scope that can be accessed in the view. e.g.

$scope.MyFunction = function(){
    // Code goes here
}

您可以将html中的函数调用为

you can call the function in the html as

<div ng-click="MyFunction()"></div>

这篇关于angularjs项目中的jQuery脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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