[英]how to pass the value of a button to angular js
我想通過函數將我的按鈕值傳遞給角度js。 我嘗試使用ng-model傳遞函數,但是在按鈕的情況下它不起作用。 所以任何人都可以提前幫助我
HTML:
<p align="center">
<a href="#category1" id="select1" ng-model="select1" data-transition="slidedown" class="ui-btn ui-corner-all" value="Category 1" ng-click="catFn1()">Category 1</a>
</p>
角js:
$scope.catFn1 = function () {
var sel1 = document.getElementById('select1').innerHTML;
alert(sel1);
sharedService.prepForBroadcast(sel1); //----- making the button value available through out the code
return dict1(sel1);
}
function dict1(sel1) {
alert(sel1);
$("#dictation1").click(function () {
window.location = "#pageeight";
});
}
我實際上想用我知道通過這種方法進行通信的工廠方法來執行控制器通信。 所以我需要通過函數傳遞我的價值。所有的答案都是有價值的。
您可以通過e.target
屬性訪問當前對象。 從那里你可以獲取value
屬性。
<a href="#category1" ng-click="catFn1($event)" data-value="Category 1" data-transition="slidedown" class="ui-btn ui-corner-all">Category 1</a>
在控制器中:
$scope.catFn1 = function (e) {
var sel1 = e.target.getAttribute('data-value');
sharedService.prepForBroadcast(sel1);
return dict1(sel1);
}
plnkr = http://plnkr.co/edit/b1bFqZdoGVNyC5KOCkJp?p=preview
ng-click的值是一個表達式,見下文:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.js"></script>
</head>
<body ng-app>
<p align="center">
<a href="#category1"
id="select1"
ng-model="select1"
data-transition="slidedown"
class="ui-btn ui-corner-all"
value="Category 1"
ng-click="catFn1();select1 = $event.target.attributes.getNamedItem('value').textContent">
Category 1
</a>
select1 = {{ select1 }}
</p>
</body>
</html>
首先,它不是一個按鈕它的錨。
嘗試這個:
<a href="url" onclick="test(this)">Text</a>
function test(control){
alert(control.innerHTML)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.