Jquery scripts in angularjs project

两盒软妹~` 提交于 2019-12-12 04:36:17

问题


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

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.

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

<!-- build:js({.tmp,app}) 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.

What can I do to solve it?


回答1:


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

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
}

you can call the function in the html as

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


来源:https://stackoverflow.com/questions/42935319/jquery-scripts-in-angularjs-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!