2016-06-28 112 views
0

我看不到我错过了这一点,但我无法用ng-click调用任何函数。ng-click在按钮上不起作用

我的HTML元素:

<div class="field"> 
    <%= f.label :foto %><br> 
    <input type="hidden" name="visitante[foto]" id="visitante_foto" ng-model="foto" ng-value="foto"/> 
    <video id="video" width="640" height="480" autoplay></video> 
    <canvas id="canvas" width="640" height="480"></canvas> 
    </br> 
    <button type="button" id="snap" ng-click="tirarFoto()">Tirar Foto</button> 
</div> 

我的角度控制器:

angular.module('controleVisitantes', ["angucomplete-alt"]) 
    .controller('controllerVisitantes', ['$scope', function ($scope) { 
    $scope.tirarFoto = function() { 

     var canvas, context; 
     canvas = document.getElementById('canvas'); 
     context = canvas.getContext('2d'); 
     context.drawImage(video, 0, 0, 640, 480); 
     $scope.foto = canvas.toDataURL(); 

    }; 
}]) 

编辑: 我使用Ruby on Rails的。 我在<div>visitantes/new.html

console.log()加入NG-应用到我的<body>和NG-控制器的工作我的功能之外,但我不能调用任何功能。

我试过<a>,<div>,<canvas>用ng-click。

+0

你能显示整个模板吗? – AranS

+0

画布元素是否具有“画布”的ID或者您是否尝试选择该元素?你的html看起来像这个''? +1对于AranS的评论,请发布您的所有代码。 –

+0

你的js看起来很好,你可能会在你的标记中遗漏一些东西,检查ng-app和ng-controller的定义。 这里是一个示例plunkr运行你的ng-click,脚本工作正常,并且你的按钮markkup很好 https://plnkr.co/edit/4kwNhD?p=info – trickpatty

回答

0

这里是一个工作plunker:https://plnkr.co/edit/4kwNhD?p=info 脚本看起来好像没什么问题。检查您的标记,希望它看起来是这样的:

<html> 
    <head> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script> 
    <script src="script.js"></script> 

    </head> 
    <body ng-app="controleVisitantes"> 
    <div ng-controller="controllerVisitantes"> 
     <button type="button" id="snap" ng-click="tirarFoto()">Tirar Foto</button> 
    </div> 
    </body> 
</html> 

另外,我强烈建议遵循风格指南和标准的命名约定为您的应用程序。 John Papa's风格指南在角度社区非常流行,但也有其他风格指南。当您的应用程序规模增大和/或您的团队变得更大时,这将是至关重要的。

0

你在index.html中链接了控制器的文件吗?

由于只有一点点的代码我只能猜测。 模板是嵌套在正确的ng控制器中还是通过正确的路由连接到控制器? 此外,如果你使用的是controllerAs语法,你将需要前缀函数调用的名称(VM例如)

<button type="button" id="snap" click="vm.tirarFoto()">Tirar Foto</button>ng-