CoffeeScript

时间:2016-05-26 19:06:16

标签: javascript ruby-on-rails angularjs coffeescript

我对Angular + CoffeeScript很新,并且正在将它用于Rails项目。我正在尝试在控制器中创建一个函数,如果我在视图中点击一个按钮就会运行。

这是html

<div id="labs">
  <div class="clients" ng-repeat="client in clients" ng-class="{first: $index == 0}">
    <h1>{{client.name}}</h1>
    <div class="labs">
      <ul>
        <li ng-repeat="lab in client.labs">
          <a ng-href="/#!/labs/{{lab.id}}/process">
            <button ng-click = "test()">Test</button>
            <span>{{lab.name}}</span>
            <span>{{lab.created_at | date:'MMMM yyyy' }}</span>
          </a>
        </li>
      </ul>
    </div>
  </div>
</div>

这是控制器

angular.module("deloitte").controller('labsCtrl', ['$scope', 'labService','labPreferencesService', ($scope, labService, labPreferencesService ) ->  

  labService.query (data) ->
    $scope.clients = data
   # console.print (clients)

       $scope.test -> console.log("Hello!");
])

这就是错误

angular.js?body=1:5755 TypeError: $scope.test is not a function
    at new <anonymous> (labsController.js?body=1:7)
    at invoke (angular.js?body=1:2903)
    at Object.instantiate (angular.js?body=1:2915)
    at angular.js?body=1:4806
    at update (angular.js?body=1:14199)
    at Object.Scope.$broadcast (angular.js?body=1:8308)
    at angular.js?body=1:7464
    at wrappedCallback (angular.js?body=1:6847)
    at wrappedCallback (angular.js?body=1:6847)
    at angular.js?body=1:6884

我查了一下语法,看起来是对的。做= - &gt;也会抛出一个错误。帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

是否要在.test()上调用$scope功能或定义它?

$scope.test -> console.log("Hello!");

// Generates
$scope.test(function() {
  return console.log("Hello!");
});

如果你想定义/分配:

$scope.test = -> console.log("Hello!");

// Generates
$scope.test = function() {
  return console.log("Hello!");
};

这可能就是你想要的。