Angular, première partie

This commit is contained in:
2015-04-08 23:22:53 +02:00
committed by Benjamin Bohard
parent a6f66208f9
commit 18aec7b86f
32 changed files with 585 additions and 35 deletions

View File

@ -0,0 +1,32 @@
/*
* Énoncé:
* Implémenter le service "myPrompt" qui pose une question et retourne la réponse entrée par l'utilisateur.
* Le service devra lui même utiliser le service $window, service "alias" de l'objet Window fourni par Angular et sa méthode
* prompt().
*
* Documentation:
* - Service $window: https://code.angularjs.org/1.3.15/docs/api/ng/service/$window
* - window.prompt(): https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt
*
*/
var Exo = angular.module('Exo', []);
Exo.controller('MainCtrl', ['$scope', 'myPrompt', function($scope, myPrompt) {
$scope.question = 'Quel est votre age ?';
$scope.response = '--';
$scope.poserQuestion = function() {
$scope.response = myPrompt.ask($scope.question);
};
}]);
Exo.service('myPrompt', ['$window', function($window) {
this.ask = function(question) {
return $window.prompt(question);
};
}]);

View File

@ -0,0 +1,21 @@
<html>
<head>
<meta charset="utf8">
<title>Exercice: création de service</title>
</head>
<!-- Déclaration de l'application -->
<body ng-app="Exo">
<div ng-controller="MainCtrl">
<label>Question</label>
<input type="text" ng-model="question" />
<button ng-click="poserQuestion()">Poser la question</button>
<br /><br />
<b>Votre réponse:</b> <span>{{ response }}</span>
</div>
<!-- Import de du framework Angular -->
<script src="../../node_modules/angular/angular.js"></script>
<script src="app.js"></script>
</body>
</html>

View File

@ -8,7 +8,6 @@
* - Service $window: https://code.angularjs.org/1.3.15/docs/api/ng/service/$window
* - window.prompt(): https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt
*
*
*/
var Exo = angular.module('Exo', []);
@ -18,6 +17,8 @@ Exo.controller('MainCtrl', ['$scope', /*'myPrompt',*/ function($scope /*,myPromp
$scope.question = 'Quel est votre age ?';
$scope.response = '--';
// scope.response = myPrompt.ask($scope.question);
$scope.poserQuestion = function() {
// $scope.response = myPrompt.ask($scope.question);
};
}]);

View File

@ -9,7 +9,8 @@
<div ng-controller="MainCtrl">
<label>Question</label>
<input type="text" ng-model="question" />
<br />
<button ng-click="poserQuestion()">Poser la question</button>
<br /><br />
<b>Votre réponse:</b> <span>{{ response }}</span>
</div>