Skip to content

Instantly share code, notes, and snippets.

@mekhami
Last active February 27, 2016 19:27
Show Gist options
  • Select an option

  • Save mekhami/bb0373fce714c79b471b to your computer and use it in GitHub Desktop.

Select an option

Save mekhami/bb0373fce714c79b471b to your computer and use it in GitHub Desktop.
angular.module('myApp', [])
.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider.
when('/', {
templateUrl: '../views/codeform.html',
controller: 'MainController'
}).
when('/confirm/:code', {
templateUrl: '../views/confirm.html',
controller: 'ConfirmController'
}).
when('/error', {
templateUrl: '../views/error.html',
});
$locationProvider.html5Mode(true);
}])
.factory('rsvpDataService', function($http) {
var service = {};
service.getRsvp = function(code) {
return $http.get('/api/rsvps/'+code.toUpperCase());
};
service.updateRsvp = function(formData) {
return $http.post('/api/rsvps/'+code.toUpperCase(), formData);
}
return service;
})
.controller('ConfirmController', ['$scope', '$routeParams', 'rsvpDataService', '$location', function($scope, $routeParams, rsvpDS, $location) {
$scope.rsvp;
getRsvp();
console.log($scope.rsvp); // Undefined?
function getRsvp() {
rsvpDS.getRsvp($routeParams.code)
.success(function(rsvp) {
merge($scope.rsvp, rsvp);
console.log(rsvp); // The correct RSVP object.
if (!rsvp) {
$location.path('/error');
};
})
.error(function(error) {
$scope.status = 'Unable to find an RSVP entry: ' + error.message;
});
}
console.log($scope.rsvp); // Undefined?
}])
.controller('MainController', ['$scope', '$location', 'rsvpDataService', function($scope, $location, rsvpDS) {
$scope.formData = {};
$scope.fetchRsvp = function() {
$location.path('confirm/' + $scope.formData.code)
};
}]);
<div id="confirm-form" class="row">
<div class="col-sm-8 col-sm-offset-2 text-center">
<form>
<div class="form-group">
<!-- BIND THIS VALUE TO formData.text IN ANGULAR -->
<input type="text" class="form-control input-lg text-center" placeholder="Enter your full name" ng-model="formData.name" ng-value="rsvp.name">
<h4>{{rsvp.email}}</h4>
<input type="text" class="form-control input-lg text-center" placeholder="Enter your Email address" ng-model="formData.email" value="{{ rsvp.email }}">
<h3>Attending?</h3>
<label>True
<input type="radio" class="form-control input-lg text-center" ng-model="formData.attending" ng-value="true">
</label>
<label>False
<input type="radio" class="form-control input-lg text-center" ng-model="formData.attending" ng-value="false">
</label>
</div>
<button type="submit" class="btn btn-primary btn-lg" ng-click="fetchRsvp()">Add</button>
</form>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment