gpt4 book ai didi

javascript - ngRepeat 中的变量在 ngClick 中不起作用

转载 作者:行者123 更新时间:2023-11-28 05:57:47 25 4
gpt4 key购买 nike

我不知道我做错了什么。应用程序通过 REST 从服务器获取对象,然后将其列在表中。一切看起来都不错,但是 ngClick 参数中的变量无法编译,因此会带来一些麻烦。

<tbody>
<tr ng-repeat="workspace in workspaces" id="workspace_{[{workspace.id}]}">
<td>{[{ workspace.name }]}</td>
<td>
<a href="javascript:void(0)" class="btn btn-info" ng-click="renameWorkspace(workspace.id)"><i class="fa fa-edit"></i></a>
<a href="javascript:void(0)" class="btn btn-danger" ng-click="deleteWorkspace(workspace.id)"><i class="fa fa-trash-o"></i></a>
</td>
</tr>
</tbody>

输出:

<tbody>
<tr ng-repeat="workspace in workspaces" id="workspace_1" class="ng-scope">
<td class="ng-binding">Work12</td>
<td>
<a href="javascript:void(0)" class="btn btn-info" ng-click="renameWorkspace(workspace.id)"><i class="fa fa-edit"></i></a>
<a href="javascript:void(0)" class="btn btn-danger" ng-click="deleteWorkspace(workspace.id)"><i class="fa fa-trash-o"></i></a>
</td>
</tr>
<tr ng-repeat="workspace in workspaces" id="workspace_2" class="ng-scope">
<td class="ng-binding">Private43243</td>
<td>
<a href="javascript:void(0)" class="btn btn-info" ng-click="renameWorkspace(workspace.id)"><i class="fa fa-edit"></i></a>
<a href="javascript:void(0)" class="btn btn-danger" ng-click="deleteWorkspace(workspace.id)"><i class="fa fa-trash-o"></i></a>
</td>
</tr>
<tr ng-repeat="workspace in workspaces" id="workspace_3" class="ng-scope">
<td class="ng-binding">iuytre</td>
<td>
<a href="javascript:void(0)" class="btn btn-info" ng-click="renameWorkspace(workspace.id)"><i class="fa fa-edit"></i></a>
<a href="javascript:void(0)" class="btn btn-danger" ng-click="deleteWorkspace(workspace.id)"><i class="fa fa-trash-o"></i></a>
</td>
</tr>
</tbody>

Angular (1.5.5):

var cerber = angular.module('cerber', ['ngRoute', 'ngResource', 'ngCookies']);

cerber.config(function($routeProvider, $locationProvider, $interpolateProvider) {
$interpolateProvider.startSymbol('{[{').endSymbol('}]}');

$routeProvider
.when('/', {
templateUrl : templatesUrlPrefix + 'group',
controller : 'mainController'
})
[...]
.otherwise({redirectTo : '/'});

$locationProvider.html5Mode(false);
});

cerber.controller('mainController', function($scope, $cookies, $location, $http, $route, $compile, GroupService, InstanceService, WorkspaceService) {
$scope.manageWorkspaces = function(){
$http({
url: responsesUrlPrefix + 'get-workspaces',
method: "GET",
params: {}
})
.then(function(response){
$scope.workspaces = response.data;
angular.element('.workspaces-manage-modal').modal('show');
});
}

回应:

[{"id":1,"name":"Work12","icon":"fa-briefcase","user_id":1,"created_at":"2016-05-16 21:01:22","updated_at":"2016-05-28 23:02:55"},{"id":2,"name":"Private43243","icon":"fa-user","user_id":1,"created_at":"2016-05-16 21:01:22","updated_at":"2016-05-28 23:02:08"},{"id":3,"name":"iuytre","icon":"fa-user","user_id":1,"created_at":"2016-05-28 23:51:23","updated_at":"2016-05-28 23:51:23"},{"id":4,"name":"iuytre","icon":"fa-user","user_id":1,"created_at":"2016-05-28 23:51:33","updated_at":"2016-05-28 23:51:33"}]

最佳答案

Angular JS 不会显示您在 DOM 中传递的参数,但会在 Controller 中为您提供正确的输出。

您可以轻松地在主 Controller 中编写接受参数的函数:

$scope.renameWorkspace = function(workSpaceId){
console.log(workSpaceId);
};

$scope.deleteWorkspace = function(workSpaceId){
console.log(workSpaceId);
}

关于javascript - ngRepeat 中的变量在 ngClick 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37508932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com