簡體   English   中英

使用AngularJS獲取HTML元素的ID

[英]Get id of the HTML element with AngularJS

我需要獲取每行的特定ID並轉發到發出HTTP請求的JS函數。 , the parameters are correct but the alert doesn't run. 但是我在調​​用此函數遇到麻煩,參數正確但警報沒有運行。
這是為什么?

的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);
    }
});

這些功能不需要{{}}就像每個人在ng-click上所說的那樣刪除它們。

像那樣 :

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

嘗試注入“ $ window”。 這樣,您將確保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);
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM