2016-06-13 2 views
0

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); 
    }; 

폼에서 상세 가지고 전달 토큰 및 부속 (전류 userDatService 빼앗아)

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;}); 

API URL로 데이터 전송

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는 사용자 또는 하위 도메인을 허용하고 URL을 반환 constants.js의 기능을 갖고 싶어?

2. 개선을 위해 base_url이나 제안 사항을 형식화하는 올바른 방법입니다.

3.I 내가 AngularJS와 자바 스크립트에 새로 온 사람 prefix + domain + postfix + ...

과 완벽한 BASE_URL를 정의 할 필요가 나는 해결책을 얻기 위해 최선을 시도했지만 기능

답변

0

그것은있을 수 있습니다 상수와 함께 작동하지 않습니다 바닐라 자바 ​​스크립트 파일에 상수를 넣고 각도 관련 스크립트가로드되기 전에 html로 스택에로드하는 더 좋은 방법입니다. 그렇게하면 그들은 이미 글로벌 네임 스페이스에있을 것이고 어디에서나 간단히 참조 할 수 있습니다.

Constant.js

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

index.html을

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

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;});