[英]how to recover the variable clicked in angularJS
當我單擊按鈕時,只需恢復(在“ myfunction”中)行中的變量“ name”
這是我的索引頁面
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr>
<th>Nom</th>
<th>Pays</th>
<th>Ville</th>
<th>button</th>
</tr>
<tr ng-repeat="x in myData">
<td value="{{x.Name}}">{{ x.Name | uppercase }}</td>
<td>{{ x.Country }}</td>
<td>{{ x.City }}</td>
<td><button ng-click="myFunction()">Click me!</button></td>
</tr>
</table>
</div>
<script src="Ctrl.js"></script>
</body>
</html>
這是我的控制器
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("customers.php").then(function (response) {
$scope.myData = response.data.records;
console.log($scope.myData);
$scope.myfunction = function(){
}
});
});
這是我的文件php
{ "records":[ {"Name":"Alfreds Futterkiste","City":"Berlin","Country":"Germany"}, {"Name":"Ana Trujillo Emparedados y helados","City":"México D.F.","Country":"Mexico"}, {"Name":"Antonio Moreno Taquería","City":"México D.F.","Country":"Mexico"}, {"Name":"Around the Horn","City":"London","Country":"UK"}, {"Name":"B's Beverages","City":"London","Country":"UK"}, {"Name":"Berglunds snabbköp","City":"Luleå","Country":"Sweden"}, {"Name":"Blauer See Delikatessen","City":"Mannheim","Country":"Germany"}, {"Name":"Blondel père et fils","City":"Strasbourg","Country":"France"}, {"Name":"Bólido Comidas preparadas","City":"Madrid","Country":"Spain"}, {"Name":"Bon app'","City":"Marseille","Country":"France"}, {"Name":"Bottom-Dollar Marketse","City":"Tsawassen","Country":"Canada"}, {"Name":"Cactus Comidas para llevar","City":"Buenos Aires","Country":"Argentina"}, {"Name":"Centro comercial Moctezuma","City":"México D.F.","Country":"Mexico"}, {"Name":"Chop-suey Chinese","City":"Bern","Country":"Switzerland"}, {"Name":"Comércio Mineiro","City":"São Paulo","Country":"Brazil"} ] }
預先感謝您的回復,
我認為您以自己的方式嘗試了此簡單的操作,但由於輸入錯誤,沒有在控制器的函數內獲取任何數據:)
從HTML內部調用函數時,您犯了一個非常愚蠢的錯誤。 您將其鍵入為myFunction()
其中,根據控制器的代碼,調用名稱應為myfunction()
。
因此,以下內容非常有效!
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr>
<th>Nom</th>
<th>Pays</th>
<th>Ville</th>
<th>button</th>
</tr>
<tr ng-repeat="x in myData">
<td value="{{ x.Name }}">{{ x.Name | uppercase }}</td>
<td>{{ x.Country }}</td>
<td>{{ x.City }}</td>
<td><button ng-click="myfunction(x.Name)">Click me!</button></td>
</tr>
</table>
</div>
<script src="Ctrl.js"></script>
</body>
</html>
與Ctrl.js
一樣,如下所示...
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("customers.php").then(function (response) {
$scope.myData = response.data.records;
console.log($scope.myData);
$scope.myfunction = function(Name){
console.log(Name);
}
});
});
Javascript區分大小寫!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.