简体   繁体   English

在angularjs常量中定义函数

[英]defining functions in angularjs constants

controller.js controller.js

angular.module('app.main')

.controller('MainCtrl', function ($scope, currentUser, addAPI) {
    $scope.form = {};
    $scope.subdomain = currentUser.domainName;

    $scope.add = function () {
        addAPI.addAdmin(localStorage['token'], $scope.subdomain, $scope.form, onSuccess, onError);
    };

take the details from the form and pass token and subdomain(took from current userDatService) 从表单中获取详细信息并传递令牌和子域(取自当前userDatService)

addAPI.js addAPI.js

angular.module('app.main').factory('addAPI', function ($resource, $http, Constant) {
var adminAPI = function () {

    this.addAdmin = function (token, domain, dataObj, sucCall, errCall) {
        $http({
            method: 'POST',
            url: Constant.API.prefix + domain + Constant.API.postfix + '/client/admin',
            headers: {
                'Token': token
            },
            data: dataObj
        }).then(handleResp).catch(handleResp);
};
return new adminAPI;});

sending data to API URL 将数据发送到API URL

constants.js constants.js

angular.module('app.constants', [])

.constant('Constant', {
        'API': {
            prefix: 'http://api.',
            postfix:'.dev.education.in/v1/academy-api/api/v.1.0'
        }
    });

1.I want to have a function in constants.js which accepts user or subdomain and returns URL? 1.我想在constants.js中有一个接受用户或子域并返回URL的函数吗?

2.is it the right way to format a base_url or any suggestions on improving. 2.是设置base_url格式或进行任何改进的正确方法吗?

3.I need to define a perfect base_url with prefix + domain + postfix + ... 3.我需要用prefix + domain + postfix + ...定义一个完美的base_url

I'm new to angularJs and Javascript and i tried my best to get a solution but functions are not working with constants 我是angularJs和Java语言的新手,我尽力获得了解决方案,但函数无法使用常量

It may be a better method to put your constants in a vanilla javascript file and load them onto the stack (via html) before any angular-related scripts are loaded. 在将所有与角度相关的脚本加载之前,将常量放入普通javascript文件中并将其加载到堆栈中(通过html)可能是一种更好的方法。 That way they will already be in the global namespace and you can simply refer to them anywhere. 这样,它们将已经在全局名称空间中,您可以在任何地方简单地引用它们。

eg 例如

Constant.js Constant.js

var API = {
    prefix: 'http://api.',
    postfix:'.dev.education.in/v1/academy-api/api/v.1.0'
}

index.html index.html

<script src="Constant.js"></script>
<script src="factories/addAPI.js"></script>

addAPI.js addAPI.js

    angular.module('app.main').factory('addAPI', function ($resource, $http, Constant) {
var adminAPI = function () {

    this.addAdmin = function (token, domain, dataObj, sucCall, errCall) {
        $http({
            method: 'POST',
            url: API.prefix + domain + API.postfix + '/client/admin',
            headers: {
                'Token': token
            },
            data: dataObj
        }).then(handleResp).catch(handleResp);
};
return new adminAPI;});

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

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