unfoudev unfoudev - 6 months ago 10
AngularJS Question

Print array elements AngularJS

I would print a string with Angular variables.

<script>
var app = angular.module('monApp',[]);
app.controller('Controleur',function($scope){$scope.infos={
"prenom": "Jean",
"nom": "Dupont",
"dateNaissance": new Date(1991,11,1)
};
}


</script>


I would like to print Hello Jean Dupont born on "1991-11-30T23:00:00.000"
I have tried many ways to print finded on web but no result.

<body data-ng-app="monApp" class ="ng-scope">
<div data-ng-controller="Controleur" class ="ng-scope" ><p>Hello {{prenom}} {{scope.infos[1], born on {{scope.infos[2]}}</p></div>

</body></html>

Answer

$scope.infos is a Object not Array, you should select to it with $scope.infos['propertyName'] or $scope.infos.propertyName

View:

<div ng-app="app">
  <div ng-controller="Controller">
    <p>Hello {{infos.prenom + ' ' + infos.nom + ', born on ' + infos.dateNaissance}}</p>
  </div>
</div>

Controller:

var app = angular.module('app', []);
app.controller('Controller', function($scope) {
  $scope.infos = {
    "prenom": "Jean",
    "nom": "Dupont",
    "dateNaissance": new Date(1991,11,1)
    };
});

See example: Here