2014-06-25 226 views
1

我想上的按鈕,點擊時訪問的輸入值:NG-點擊訪問其他元素值

控制器:

app.controller('MainCtrl', function($scope) { 
    $scope.test = function(val) { 
    alert(val); 
    } 
}); 

HTML:

<body ng-controller="MainCtrl"> 
    <input type="text" id="inputText"> 
    <button type="button" ng-click="test(document.getElementById('inputText').value)">Test</button> 
</body> 

但'未定義'是警報中出現的內容。這是如何實現的角度?

回答

5

隨着ng-model上所需input

<input type="text" id="inputText" ng-model="myInput"> 
<button type="button" ng-click="test(myInput)">Test</button> 
0

但是,如果您切換的順序一樣,

<button type="button" ng-click="test(myInput)">Test<//button> 

<input type="text" id="inputText" ng-model="myInput"> 

你不會得到myInput值。在這種情況下,你會發現它很奇怪!順便說一下,如果你把你的按鈕在div欄中,也許你會得到這個問題

+0

Olla!我得到解決方案。放入你的控制器\t \t ... angular.element(document.getElementById('inputText')); ... – user3130270