Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Angular dynamiczne ng-messages
Forum PHP.pl > Forum > Po stronie przeglądarki
smiady
Witam. Mam problem z dynamicznie dodawanym ng-messages

  1. <!DOCTYPE html>
  2. <html lang="pl">
  3. <head>
  4. <meta charset="utf8" />
  5. <link type="text/css" rel="stylesheet" href="css/bootstrap.css" />
  6. <title>dynamic</title>
  7. </head>
  8. <body>
  9. <div ng-app="app">
  10. <div ng-controller="AppCtrl" class="container">
  11. <div class="row">
  12. <div class="col-sm-6">
  13. <div class="panel panel-primary">
  14. <div class="panel-heading">
  15. <span class="glyphicon glyphicon-leaf"></span> Inputs
  16. </div>
  17. <div class="panel-body">
  18. <form id="myForm" class="form-horizontal" name="myForm">
  19. <div class="form-group" ng-repeat="(key, input) in inputs">
  20. <label for="myInput{{key}}" class="col-sm-4 control-label">Input {{key}}</label>
  21. <div class="col-sm-8">
  22. <input type="email" id="myInput{{key}}" class="form-control"
  23. ng-model="input.name" name="myInput{{key}}" required />
  24. <div ng-messages="myForm.myInput{{key}}.$error">
  25. <div ng-message="required">This field is required.</div>
  26. <div ng-message="email">This email is required.</div>
  27. </div>
  28. </div>
  29. </div>
  30. </form>
  31. </div>
  32. <div class="panel-footer clearfix">
  33. <button type="button" class="btn btn-success pull-right" ng-click="addInput()">
  34. <span class="glyphicon glyphicon-plus"></span> Add input
  35. </button>
  36. </div>
  37. </div>
  38. </div>
  39. <div class="col-sm-6">
  40.  
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45.  
  46. <script type="text/javascript" src="jquery.js"></script>
  47. <script type="text/javascript" src="bootstrap.js"></script>
  48. <script type="text/javascript" src="angular.js"></script>
  49. <script type="text/javascript" src="angular-route.js"></script>
  50. <script type="text/javascript" src="angular-resource.js"></script>
  51. <script type="text/javascript" src="angular-messages.js"></script>
  52. <script type="text/javascript">
  53. angular.module('app', ['ngMessages']);
  54.  
  55. angular.module('app').controller('AppCtrl', function($scope) {
  56. $scope.inputs = [{
  57. id: 1,
  58. name: 'example'
  59. }, {
  60. id: 2,
  61. name: 'test'
  62. }, {
  63. id: 3,
  64. name: 'custom'
  65. }];
  66.  
  67. $scope.addInput = function() {
  68. $scope.inputs.push({
  69. id: 0,
  70. name: ''
  71. });
  72. };
  73. });
  74. </script>
  75. </body>
  76. </html>


Problem występuje we fragmencie <div ng-messages="myForm.myInput{{key}}.$error"> w taki sposób nie można się odwołać do $error, choć obiekt myForm.myInput{{key}} istnieje, bo sprawdzałem w console.log.
Co powinno być w ng-messages ?
marcio
Raczej powinno byc:
  1. <div ng-messages="myForm.myInput[key].$error">
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.