2017-04-19 53 views
0

我試圖通過單擊按鈕來刷新驗證碼。但它不會進入功能本身。我錯過了什麼?這裏是我的controller.js代碼:ng-click不會輸入函數本身

$scope.goCaptcha = function() 
{ 
console.log('entered'); 
var alpha = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4','5','6','7','8','9','0'); 

var i; 
for (i = 0; i < 6; i++) { 
    var a = alpha[Math.floor(Math.random() * alpha.length)]; 
    var b = alpha[Math.floor(Math.random() * alpha.length)]; 
    var c = alpha[Math.floor(Math.random() * alpha.length)]; 
    var d = alpha[Math.floor(Math.random() * alpha.length)]; 
    var e = alpha[Math.floor(Math.random() * alpha.length)]; 
    var f = alpha[Math.floor(Math.random() * alpha.length)]; 
    var g = alpha[Math.floor(Math.random() * alpha.length)]; 
} 
var code = a + ' ' + b + ' ' + ' ' + c + ' ' + d + ' ' + e + ' ' + f + ' ' + g; 

document.getElementById("main-captcha").value = code 
} 

,這裏是我的html代碼:

<label class="item item-input" id="login-input3"> 
    <input type="text" readonly id="main-captcha"> 
    <button type="submit" class="button button-small" ng-click="goCaptcha()">Refresh</button> 
</label> 
+0

看起來不錯,但也許你的模板還沒有分配給它的正確的控制器。 –

回答

0

它的工作。您可能會錯過HTML中的ng-controller="yourCtrlName"。或檢查是否有其他愚蠢的錯誤。

工作片斷:

var app = angular.module('plunker', []); 
 

 
app.controller('MainCtrl', function($scope) { 
 
    $scope.goCaptcha = function() { 
 
    console.log('entered'); 
 
    var alpha = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'); 
 

 
    var i; 
 
    for (i = 0; i < 6; i++) { 
 
     var a = alpha[Math.floor(Math.random() * alpha.length)]; 
 
     var b = alpha[Math.floor(Math.random() * alpha.length)]; 
 
     var c = alpha[Math.floor(Math.random() * alpha.length)]; 
 
     var d = alpha[Math.floor(Math.random() * alpha.length)]; 
 
     var e = alpha[Math.floor(Math.random() * alpha.length)]; 
 
     var f = alpha[Math.floor(Math.random() * alpha.length)]; 
 
     var g = alpha[Math.floor(Math.random() * alpha.length)]; 
 
    } 
 
    var code = a + ' ' + b + ' ' + ' ' + c + ' ' + d + ' ' + e + ' ' + f + ' ' + g; 
 

 
    document.getElementById("main-captcha").value = code 
 
    } 
 
});
<!DOCTYPE html> 
 
<html ng-app="plunker"> 
 

 
<head> 
 
    <meta charset="utf-8" /> 
 
    <title>AngularJS Plunker</title> 
 
    <script> 
 
    document.write('<base href="' + document.location + '" />'); 
 
    </script> 
 
    <script data-require="[email protected]" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script> 
 
</head> 
 

 
<body ng-controller="MainCtrl"> 
 
    <label class="item item-input" id="login-input3"> 
 
    <input type="text" readonly id="main-captcha"> 
 
    <button type="submit" class="button button-small" ng-click="goCaptcha()">Refresh</button> 
 
</label> 
 
</body> 
 

 
</html>