来自ngRepeat的变量在ngClick中不起作用

时间:2016-05-29 10:44:13

标签: javascript html angularjs angularjs-ng-repeat angularjs-ng-click

我不知道我做错了什么。应用程序通过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"}]

1 个答案:

答案 0 :(得分:0)

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

您可以在主控制器中轻松编写函数,该函数接受以下参数:

val it : unit = ()

>