Angularjs - ng-repeat is not working

Asked

Viewed 250 times

0

Good afternoon, I made the following code: http://jsfiddle.net/27du7oaL/5/ . Accessing Jsfiddle, you can see that Angular shows the title that is in the controller, but does not show any data related to the array. How to solve this problem?

HTML

<div ng-app="Series">
  <div class="container" ng-controller="SeriesController">
    <h1>{{titulo}}</h1>
    <form autocomplete="off" ng-submit="adicionaSerie()">
      <div class="form-group">
        <label for="nome">Nome da serie:</label>
        <input type="text" pattern="{1,}" placeholder="Minímo 1 caractere" id="name" ng-model="novaSerie.nome" required>
      </div>
      <div class="form-group">
        <label for="produtora">Produra:</label>
        <input type="text" ng-model="novaSerie.produtora">
      </div>
      <footer><button type="submit">Adicionar serie</button></footer>
    </form> 
  </div>

  <ul>
    <li ng-repeat="serie in series">
    <h3>{{serie.nome}}</h3>
    <h3>{{serie.produtora}}</h3>
    </li>
  </ul> 

</div>

Angularjs :

var app = angular.module("Series", []);

app.controller("SeriesController", function($scope){

    $scope.titulo = "Adiciona Serie";

    $scope.series = [
        {
            nome: 'Nothing',
            produtora: 'Nothing Producers'

        }
    ];

    $scope.novaSerie = {};

    $scope.adicionaSerie = function() {
        var serie = angular.copy($scope.novaSerie);
        $scope.artistas.push(serie);
        $scope.novaSerie = {};
    }
});

1 answer

1


There are two mistakes there:

  • The list that should contain the $scope.series is out of of div containing the Directive ne-controller.

  • In function adicionaSerie has the following line:

    $scope.artistas.push(serie);
    

    That should be

    $scope.series.push(serie);
    

See the code working:

var app = angular.module("Series", []);

app.controller("SeriesController", function($scope) {

  $scope.titulo = "Adiciona Serie";
  $scope.novaSerie = {};
  $scope.series = [
    {
      nome: 'Nothing',
      produtora: 'Nothing Producers'
    }
  ];

  $scope.adicionaSerie = function() {
    var serie = angular.copy($scope.novaSerie);
    $scope.series.push(serie);
    $scope.novaSerie = {};
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="Series" ng-controller="SeriesController">
  <div class="container" >
    <h1>{{titulo}}</h1>
    <form autocomplete="off" ng-submit="adicionaSerie()">
      <div class="form-group">
        <label for="nome">Nome da serie:</label>
        <input type="text" pattern="{1,}" placeholder="Minímo 1 caractere" id="name" ng-model="novaSerie.nome" required>
      </div>
      <div class="form-group">
        <label for="produtora">Produra:</label>
        <input type="text" ng-model="novaSerie.produtora">
      </div>
      <footer>
        <button type="submit">Adicionar serie</button>
      </footer>
    </form> 
  </div>

  <ul>
    <li ng-repeat="serie in series">
      <h3>{{serie.nome}}</h3>
      <h3>{{serie.produtora}}</h3>
    </li>
  </ul> 
</div>

  • Thank you, I went to make a different example about my problem, and I didn’t realize that in it I put off the div, but in the original had put the ng-controller in a global div of the site there had not worked at all, worse that I only came to find out after I was looking at everything right and I realized that I had committed that I had put '; ' in place of ',' in the separation of attributes of an object... but then q corrected it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.