简体   繁体   English

从另一个文件AngularJS中的另一个Controller调用函数

[英]Call function from another Controller in another file AngularJS

Call function from another Controller Angular Js 从另一个Controller Angular Js调用函数

Call function from another controller Angularjs 从另一个控制器Angularjs调用函数

http://tutorials.jenkov.com/angularjs/dependency-injection.html http://tutorials.jenkov.com/angularjs/dependency-injection.html

I've been googling around and trying to adapt but I can't figure out. 我一直在四处搜寻并尝试适应,但我不知道。 I have two files: home.js and auth.js . 我有两个文件: home.jsauth.js I created the file auth.js because I will need to call the functions inside of it in lot of files. 我创建了文件auth.js因为我需要在许多文件中调用其中的函数。

I can't create simple functions like: 我无法创建简单的函数,例如:

function isAuthed(){
  return true;
}

Because I'll need to use some AngularJS modules, such as HTTP, therefore I believe I need to create a decent angular js file. 因为我需要使用一些AngularJS模块(例如HTTP),所以我相信我需要创建一个不错的angular js文件。 For what I've read so far, in order to call a function from another controller the best and correct way is to use Services . 到目前为止,为了从另一个控制器调用函数,最好的正确方法是使用Services

home.js home.js

var app = angular.module('home', [
    'ngRoute'
]);

app.controller('home', ['$scope', 'authSERVICE', function($scope, authSERVICE){
    console.log(authSERVICE.hasCredentials());
}]);

auth.js 验证码

var app = angular.module('auth', []);

app.service('authSERVICE', ['$http', function($http){

    this.hasCredentials = function(){

        if(window.localStorage.getItem('email') != null && window.localStorage.getItem('password') != null){
            return true;
        }   
    }

    this.login = function(email, password){

        // Base64 function is from a simple JS file.
        var auth = Base64.encode('username:password');

        $http.get('http://localhost/project/api/webservice/getCategories', {

            headers: { 'Authorization': 'Basic ' + auth },
            params : {
                email: email,
                password: password,
            }

        }).success(function(response){

            return true;

        }).error(function(response){

            return false;

        }); 
    }
}]);

This does not work and I receive an error on console poiting to: https://docs.angularjs.org/error/$injector/unpr?p0=authSERVICEProvider%20%3C-%20authSERVICE I kind of understand what's the error information telling me. 这不起作用,我在控制台上输入以下错误: https ://docs.angularjs.org/error/$injector/unpr?p0 = authSERVICEProvider%20%3C-%20authSERVICE我有点理解错误信息的含义我。 Although, I still need and want to have two different files. 虽然,我仍然需要并且想要拥有两个不同的文件。

EDIT : Btw, in the index.html file I've called the scripts: 编辑 :顺便说一句,在index.html文件中,我将脚本称为:

<script type="text/javascript" src="application/controllers/home.js"></script>
<script type="text/javascript" src="application/controllers/auth.js"></script>

Thanks to @Ved answer I manage to figure it out. 感谢@Ved答案,我设法弄清楚了。 Solved . 解决了

var app = angular.module('home', [
    'ngRoute',
    'auth'
]);

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

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