如何在 ng-click 中传递范围变量以及如何访问控制器中 angularJS 函数中的参数
How to Pass Scope Variable in ng-click and how to access the Parameter in angularJS Function within Controller
如何在 ng-click 中传递范围变量以及如何访问控制器中 angularJS 函数中的参数?
我的源代码是
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td><a href="" ng-click="SuperFunction('{{x.Name}}')">{{ x.Country }}</a></td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://www.w3schools.com/angular/customers.php")
.then(function (response) { $scope.names = response.data.records; });
$scope.SuperFunction = function (id) {
alert(id);
};
});
</script>
</body>
</html>
点击时我的输出屏幕截图
Output Screen Shot in Firefox with Inspect Element Window
我得到的参数值是 {{x.Name}}
而不是实际值。
错误在这里:
ng-click="SuperFunction('{{x.Name}}')"
应该是:
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td><a href="" ng-click="SuperFunction(x.Name)">{{ x.Country }}</a></td>
</tr>
</table>
根据建议更改。它会起作用。
如何在 ng-click 中传递范围变量以及如何访问控制器中 angularJS 函数中的参数?
我的源代码是
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td><a href="" ng-click="SuperFunction('{{x.Name}}')">{{ x.Country }}</a></td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://www.w3schools.com/angular/customers.php")
.then(function (response) { $scope.names = response.data.records; });
$scope.SuperFunction = function (id) {
alert(id);
};
});
</script>
</body>
</html>
点击时我的输出屏幕截图 Output Screen Shot in Firefox with Inspect Element Window
我得到的参数值是 {{x.Name}}
而不是实际值。
错误在这里:
ng-click="SuperFunction('{{x.Name}}')"
应该是:
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td><a href="" ng-click="SuperFunction(x.Name)">{{ x.Country }}</a></td>
</tr>
</table>
根据建议更改。它会起作用。