-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
53 lines (46 loc) · 1.8 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//creating this allows for avoiding global variables
(function(){
var app = angular.module('funwithcountries',['ngRoute']);
app.factory('countryService',function($http){
var baseUrl = 'services/';
return{
getCountries: function(){
return $http.get(baseUrl + 'getCountries.php')
},
getStates: function(countryCode) {
return $http.get(baseUrl + 'getStates.php?countryCode=' +
encodeURIComponent(countryCode));
}
};
});
app.controller('CountryController', function(countryService){
var that = this;
countryService.getCountries().success(function(data){
//this.countries = data; will not work because this
//refers to the window object, not the controller instance
that.countries = data;
});
});
app.config(function($routeProvider) {
$routeProvider.when('/states/:countryCode', {
templateUrl: 'state-view.html',
controller: function ($routeParams, countryService) {
this.params = $routeParams;
var that = this;
countryService.getStates(this.params.countryCode || "").success(function (data) {
that.states = data;
})
this.addStateTo = function () {
//adding an array for the countries that don't currently
//have states
if (!this.states) {
this.states = [];
}
this.states.push({name: this.newState});
this.newState = ""; //clears the state after it's added
};
},
controllerAs: 'stateCtrl'
});
});
})();