如何将json条目传入我的onClick ="警告( x.AlertInfo )" JSON返回数组的区域?
尝试激活一个消息框,该消息框返回整个行的onClick返回的JSON条目的字符串。特别是这个条目。表的其余部分工作正常,只是这一部分。
<tr ng-repeat="x in json.tags | filter:filterName | filter:filterID | orderBy:myOrderBy:reverse" onClick="alert(x.AlertInfo)">
这是完整的源代码。
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>
<div ng-app="myApp" ng-controller="getJson">
<!-- <p>Status : {{statuscode}}</p>
<p>StatusText : {{statustext}}</p>-->
<input type="text" id="input" ng-model="filterName" placeholder="Search.." title="Type in a name">
<table id="myTable">
<tr>
<th ng-click="orderByMe('t1')" width="8%">t1</th>
<th ng-click="orderByMe('t2')" width="5%">t2</th>
<th ng-click="orderByMe('t3')" width="3%">t3</th>
</tr>
<tr ng-repeat="x in json.tags | filter:filterName | filter:filterID | orderBy:myOrderBy:reverse" onClick="alert(x.AlertInfo)">
<td>{{x.t1}}</td>
<td>{{x.t2}}</td>
<td>{{x.t3}}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('getJson', function($scope, $http, $interval) {
$scope.getData = function() {
$http.get('http://JSONGET').
then(function(response) {
$scope.statuscode = response.status;
$scope.statustext = response.statusText;
$scope.json = response.data;
console.log('Feteched data!');
});
};
$scope.orderByMe = function(x) {
$scope.myOrderBy = x;
};
$scope.intervalFunction = function(){
$scope.getData();
$interval(function(){
$scope.getData();
}, 15000);
};
$scope.intervalFunction()
});
</script>
答案 0 :(得分:2)
您不能将警报和控制台作为html中的表达式调用。您可以做的是为控制器中的范围功能分配警报
$scope.alert = window.alert;
现在从html调用警报。并使用ng-click
代替onClick
。
<tr ng-repeat="x in json.tags | filter:filterName | filter:filterID | orderBy:myOrderBy:reverse" ng-click="alert(x.AlertInfo)">
答案 1 :(得分:2)
使用$winodw
服务和ng-click
,警报始终以ng-click工作,但是如果您想操纵某些数据。
app.controller('getJson', function($scope, $http, $interval, $window) {
// content and methods
// ...
// ...
$scope.alertInfo = function(index) {
var jsonData = $scope.json[index]; // json data of the clicked table-row
$window.alert(jsonData.AlertInfo); // can manipulate, convert object to string or do whatever you want
});
});
<tr ... data-ng-click="alertInfo($index)">
...
</tr>