1

我不知道我做錯了什麼。應用程序通過REST從服務器獲取對象,然後將其列在表中。一切看起來都很不錯,但ngClick參數中的變量不會編譯,所以會造成一些麻煩。ngRepeat的變量在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> 

角(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

*「變量ngClick說法沒有編譯所以它使一些麻煩」 * - 什麼煩惱?它不應該編譯和輸出將是'ng-click =「deleteWorkspace(workspace.id)」',它應該如何。 – dfsq

+0

您的功能:renameWorkspace,deleteWorkspace是否在您的服務WorkspaceService? – NotBad4U

+0

@NowBad4U在MainController'$ scope.deleteWorkspace = function(workspaceId)' – Domin1992

回答

0

角JS不會告訴你,你正在傳遞的參數在DOM中,但會給你正確的答案在控制器中輸出。

你可以隨便寫在接受參數作爲主控制器功能:

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

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

他已經在MainCtrl中編寫了這些函數。 「MainController中的@ NotBad4U $ scope.deleteWorkspace = function(workspaceId」 – NotBad4U