2012-12-20 128 views
1

即使簡單地使用angular-ui啓動並運行,我也遇到了麻煩。我希望能夠輕鬆檢測按鍵,例如,在文本框中按enter後自動添加項目,而無需按下添加按鈕。Angular-UI ui-keypress無法正常工作


這是我當前的嘗試:

<!DOCTYPE html> 
<html ng-app xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Main</title> 
    <link rel="stylesheet", href="http://angular-ui.github.com/angular-ui/build/angular-ui.css" /> 
</head> 
<body ng-controller="Ctrl"> 
    <button ng-click="add()">Add</button> 
    <input type="text" ui-keypress="{enter: 'add()'}" /> 
    {{item}} 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"> </script> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script> 
    <script src="http://angular-ui.github.com/angular-ui/build/angular-ui.js"></script> 
    <script src="main.js"></script> 
</body> 
</html> 

var myApp = angular.module('myApp', ['ui.directives']); 

function Ctrl($scope) { 
    $scope.item = ""; 

    $scope.add = function() { 
    $scope.item = "Item Added"; 
    } 
} 

您可以在這裏看到的行爲:http://jsfiddle.net/NbjZL/5/。請注意,在輸入文字後單擊按鈕,但在輸入文字後按enter則不行。我已經閱讀了我可以找到的文檔並查看了幾個示例,但我確定我仍然錯過了一些小東西。

回答

7

Angular ui無法找到角度應用程序。您只需在ng-app中指定應用程序名稱即可運行。

<html ng-app="myModule" xmlns="http://www.w3.org/1999/xhtml"> 

檢查js fiddle看到代碼工作

+0

唉!我不知道那個小小的屬性最終會咬我。先生,很多榮譽! – jtheis