0

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>

2 Answers 2

2

you associate your data with scope infos variable, so a proper use would be:

<div data-ng-controller="Controleur" class ="ng-scope" ><p>Hello {{infos.prenom}} {{infos.nom}}, born on {{infos.dateNaissance}}</p></div>

tip: if your don't intend to change your data after inserting it, you can make use of Angular's one-time bindings, which will improve performance

<div data-ng-controller="Controleur" class ="ng-scope" ><p>Hello {{::infos.prenom}} {{::infos.nom}}, born on {{::infos.dateNaissance}}</p></div>
Sign up to request clarification or add additional context in comments.

2 Comments

I get Hello {{infos.prenom}} {{infos.nom}}, born on {{infos.dateNaissance}}
@unfoudev that's probably because you have syntax error in your javascript, you forgot about ); at the end of your code
1

$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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.