简体   繁体   English

CoffeeScript无法识别角度控制器功能

[英]Angular controller function is not recognized in CoffeeScript

I am fairly new to Angular + CoffeeScript and am using it for a Rails project. 我对Angular + CoffeeScript相当陌生,并将其用于Rails项目。 I am trying to create a function in the controller, which runs if I hit a button in a view. 我正在尝试在控制器中创建一个函数,如果我在视图中按了一个按钮,该函数将运行。

This is the html 这是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>

This is the Controller 这是控制器

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!");
])

And this is the error 这是错误

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

I looked up the syntax and it seems correct . 我查了一下语法,这似乎是正确的。 Doing =-> throws an error too. 进行=->也会引发错误。 Help would be appreciated 帮助将不胜感激

Do you want to call .test() function on your $scope or define it? 您是否要在$scope上调用.test()函数或对其进行定义?

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

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

If you want to define/assign: 如果要定义/分配:

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

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

and that's probably what you want. 这可能就是您想要的。

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

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