angular.js - angular,公共的代码你们是放在哪里的
给我你的怀抱
给我你的怀抱 2017-05-15 17:01:00
0
1
582

我最开始是放在rootScope,发现这是全局属性,就放弃了
又不想在每个需要用到的controller里面都写一遍,之后我选择放入指令directive里面的controller里面,之后,我又发现,directive是依赖HTML的,如果方法一样,但是我HTML不一样,指令就没办法用来了。
说得有点乱,我的意思是:我的一个方法所有的地方都可能用得到,我需要放在哪里?以后用得上的时候直接调用方法。
比如:把它作为公共的代码,应该怎么写

给我你的怀抱
给我你的怀抱

reply all (1)
淡淡烟草味

Best to useservice或者factory

// use factory angular.module('YourAppName') .factory('YourFuncName', function() { return function() { // your function code here } }); // use service angualr.module('YourAppName') .service('myUtils',function() { this.yourFuncName = function() { // your function code here } })

For the situation in the screenshot

angular.module('YourAppName') .factory('YourFuncName', function() { return function($scope) { return function(modal) { // Use $scope Here } } }); // 使用时 somthing.then(yourFuncName($scope))
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!