2016-03-22 10 views
1

私は、次の工場で持っている:私はエラーが表示さ角度のJS構文エラー

(function() { 
angular.module('temp') 
    .factory('Factory',Factory); 

    employeeFactory.$inject = ['$http']; 
    function employeeFactory($http) { 
     var factory = {}; 
     var vm = this; 

     factory.login = function(email,password) { 
      return $http({ 
       'method': 'POST', 
       'url': 'http://domain.dev/api/v1/login', 
       'data': $.param({ 
        'email': email, 
        'password': password 
       }), 
       'headers': { 
        'Content-Type': 'application/x-www-form-urlencoded' 
       } 
      }); 
     return factory; 
    } 
})(); 

を:

})(); 

答えて

5

ラン:

Uncaught SyntaxError: Unexpected token) 

コンソールは、最後の行を参照空白を修正するためのコードフォーマッタを使用してコードを作成します。問題は明らかになります。あなたは1行目に開始した関数式を閉じるために使用しようとしている

(function() { 
     angular.module('skindustries') 
      .factory('employeeFactory', employeeFactory); 

     employeeFactory.$inject = ['$http']; 

     function employeeFactory($http) { 
      var factory = {}; 
      var vm = this; 

      factory.login = function(email, password) { 
       return $http({ 
        'method': 'POST', 
        'url': 'http://domain.dev/api/v1/login', 
        'data': $.param({ 
         'email': email, 
         'password': password 
        }), 
        'headers': { 
         'Content-Type': 'application/x-www-form-urlencoded' 
        } 
       }); 
       return factory; 
      } 
     })(); 

あなたの最後の}は、実際にemployeeFactory機能を閉じています。

をスクリプトの最初の文字である(と一致させる前に、1行目で開始した関数式を閉じるには別の}が必要です。

おそらく行方不明}はあなたがfactory.loginに割り当てる無名関数に属する1つであるように見えるようreturn factory;文の前にそれを載せていきたいと思います。あなただけのリターン工場

(function() { 
    angular.module('skindustries') 
    .factory('employeeFactory', employeeFactory); 

    employeeFactory.$inject = ['$http']; 

    function employeeFactory($http) { 
    var factory = {}; 
    var vm = this; 

    factory.login = function(email, password) { 
     return $http({ 
     'method': 'POST', 
     'url': 'http://domain.dev/api/v1/login', 
     'data': $.param({ 
      'email': email, 
      'password': password 
     }), 
     'headers': { 
      'Content-Type': 'application/x-www-form-urlencoded' 
     } 
     }); 
    } 
    return factory; 
    } 
})(); 
+0

'factory.login'内部の2つのreturn文の意味は何であるJohn PapaによってAngular style guide上の非常に良いガイドがありますか? –

+0

助けてくれてありがとう! – Jamie

+0

@ JenishRabadiya - 最初の 'return'は基本的に' POST'リクエストからの応答を返します。一方、第2の「返却」は、戻り値は、工場とそれに関連するAPIを公開します。上の例では、このサービスがあなたのコントローラサービスなどに依存して注入されたときに、あなたのアプリ内の他のコンポーネントに 'login(u、p)'関数を公開しています。 –

0

(function() { 
    angular.module('skindustries').factory('employeeFactory', employeeFactory); 
    employeeFactory.$inject = ['$http']; 
    function employeeFactory($http) { 
     var factory = {}; 
     var vm = this; 
     factory.login = function(email, password) { 
      return $http({ 
       'method': 'POST', 
       'url': 'http://domain.dev/api/v1/login', 
       'data': $.param({ 
       'email': email, 
       'password': password 
       }), 
       'headers': { 
       'Content-Type': 'application/x-www-form-urlencoded' 
       } 
      }); 
     } 
     return factory; 
    } 
})(); 
+0

これはエラーになります。 JS Hintレポート:識別子が必要で、代わりにsaw ')'が必要です。 – Quentin

+0

@クエンティンうん、私はそれを修正した。 –

1

前に、あなたはこれを試してみてください}

を逃し}欠けたた

0

私はそれがよりにするためにこれを自己の呼び出し機能を置き換えます読める。

(function() { 
    angular.module('skindustries').factory('employeeFactory', ['$http', function ($http) { 

     function login(email, password) { 
     return $http({ 
      'method': 'POST', 
      'url': 'http://domain.dev/api/v1/login', 
      'data': $.param({ 
      'email': email, 
      'password': password 
      }), 
     'headers': { 
      'Content-Type': 'application/x-www-form-urlencoded' 
     } 
     }); 
    } 


    // expose public api 
    return { 
     login: login 
    }; 
    }])}); 

ここ