AngularJS控制器注入咖啡脚本生成错误

时间:2014-10-14 14:48:20

标签: angularjs ruby-on-rails-4 coffeescript

我试图理解为什么以下coffeescript生成一个javascript,并将引用$ resource变量的语句放在函数范围之外。

咖啡脚本:

controllers = angular.module('controllers',[])
controllers.controller("UsersController", [ '$scope', '$routeParams',
'$resource',   ($scope,$routeParams,$resource)->    Recipe =
$resource('/recipes/:recipeId', { recipeId: "@id", format: 'json' })
    Recipe.query(keywords: $routeParams.keywords, (results)->
$scope.recipes = results)

])

生成的代码:

controllers = angular.module('controllers', []);

  controllers.controller("UsersController", [
    '$scope', '$routeParams', '$resource', function($scope, $routeParams, $resource) {}, Recipe = $resource('/recipes/:recipeId',
{
      recipeId: "@id",
      format: 'json'
    }), Recipe.query({
      keywords: $routeParams.keywords
    }, function(results) {
      return $scope.recipes = results;
    })   ]);

}).call(this);

1 个答案:

答案 0 :(得分:0)

您问题中格式化的coffeescript无效(我收到了无法比拟的OUTDENT错误)。使用下面的格式会产生正确的结果:

<强> CoffeeScript的:

controllers = angular.module('controllers',[])
controllers.controller("UsersController", [ '$scope', '$routeParams','$resource', ($scope,$routeParams,$resource) -> 
    Recipe = $resource('/recipes/:recipeId', { recipeId: "@id", format: 'json' })
    Recipe.query(keywords: $routeParams.keywords, (results)-> $scope.recipes = results)
])

生成的JavaScript:

(function() {
  var controllers;

  controllers = angular.module('controllers', []);

  controllers.controller("UsersController", [
'$scope', '$routeParams', '$resource', function($scope, $routeParams, $resource) {
      var Recipe;
      Recipe = $resource('/recipes/:recipeId', {
        recipeId: "@id",
        format: 'json'
      });
      return Recipe.query({
        keywords: $routeParams.keywords
      }, function(results) {
        return $scope.recipes = results;
      });
    }
  ]);

}).call(this);