2015-07-13 105 views
0

我需要獲取每行的特定id並轉發到將發出http請求的JS函數。但我在調用此函數時遇到問題, excluir(id),參數是正確的,但警報不運行。
這是爲什麼?使用AngularJS獲取HTML元素的ID

HTML

<!DOCTYPE html> 
<html ng-app="oknok"> 
<head lang="pt-br"> 
    <meta charset="UTF-8"/> 
    <title>OKNOK Admin</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script> 
    <script src="js/controller.js"></script> 
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> 
</head> 

<body ng-controller="indexController" data-ng-init="init()"> 
<div class="container"> 
    <h2>Listagem de ve&iacute;culos</h2> 
    <table class="table table-condensed"> 
     <thead> 
     <tr> 
      <th>Nome</th> 
      <th>Tipo</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr ng-repeat="x in resultado"> 
      <td ng-model="nome">{{x.nome}}</td> 
      <td ng-model="tipo">{{x.tipo}}</td> 
      <td ng-model="lixeira"><span class="glyphicon glyphicon-trash" ng-click="excluir({{x._links.self.href}})"></span></td> 
     </tr> 
     </tbody> 
    </table> 

<div align="right"> 
<button type="button" class="btn btn-primary" ng-click="adicionarNovo()">Adicionar novo</button> 
</div> 
</div> 
</body> 
</html> 

Controller.js

var oknok = angular.module('oknok', []); 

oknok.controller('indexController', function ($scope, $http) { 
    $scope.init = function() { 
     $http.get("/api/veiculos") 
      .then(function (data) { 
       var embedded = data.data._embedded; 
       $scope.resultado = embedded.veiculos; 
      }).catch(function (error) { 
       alert("Erro ao obter dados!\n" + error); 
      }); 
    }; 

    $scope.adicionarNovo = function() { 
     window.location.href = "/cadastro"; 
    }; 

    $scope.excluir = function (id) { 
     alert("clicou" + "\t" + id); 
    } 
}); 
+3

excluir(x._links.self.href);在ng-click內刪除雙括號 –

+2

嘗試將excluirs({{x._links.self.href}})替換爲excluir(x._links.self.href)。 如果它不能解決它,請告訴我們在控制檯日誌日誌 – kdlcruz

+0

當你說「ID」你的意思是對象的身份證,例如跨度?你爲什麼不使用它呢?像這樣:(excluir(this)),所以你可以得到span對象。 –

回答

0

嘗試注射 「$窗口」 代替。這樣的話,你一定會window對象具有正確的角度生命週期:

var oknok = angular.module('oknok', []); 

oknok.controller('indexController', function ($scope, $http, $window) { 
    $scope.init = function() { 
     $http.get("/api/veiculos") 
      .then(function (data) { 
       var embedded = data.data._embedded; 
       $scope.resultado = embedded.veiculos; 
      }).catch(function (error) { 
       $window.alert("Erro ao obter dados!\n" + error); 
      }); 
    }; 

    $scope.adicionarNovo = function() { 
     $window.location.href = "/cadastro"; 
    }; 

    $scope.excluir = function (id) { 
     $window.alert("clicou" + "\t" + id); 
    } 
}); 
2

的功能不需要{{}}大家都在ng-click之前所說的將其刪除。

喜歡的是:

ng-click="excluir(x._links.self.href)"