Last active
April 1, 2016 12:59
-
-
Save biganth/00173d935e5f98daa313 to your computer and use it in GitHub Desktop.
Angular hitting Web API 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = angular.module('CarFinderApp', []); | |
app.controller('DisplayController', ['$scope', '$http', function ($scope, $http) { | |
$scope.selected = { | |
year: '', | |
make: '', | |
model: '', | |
trim: '' | |
}; | |
} | |
]); | |
var onCompletion = function(response) { | |
$scope.response = response.data; | |
} | |
//Factory declaration for module - this provides the connectivity to the Web API controller and actions | |
app.factory('carSvc', ['$http', function ($http){ | |
var factory = {}; | |
factory.getYears = function(year){ | |
return $http.get('api/years') | |
.then(function (onCompletion, onError) | |
)}; | |
factory.getMakes = function(year, make){ | |
var options = { params: { model_year: year, make: make }}; | |
return $http.get('api/makes', options) | |
.then(function (response) { return response.data; }); | |
}; | |
}]); | |
//directive declaration for module | |
app.directive('carFinder', ['carSvc', function (carSvc) { | |
return{ | |
//define usage restrictions | |
restrict: 'AEC', | |
//bind scope variable and attributes | |
scope:{ | |
selectedYear: '=year' | |
}, | |
//define html template | |
templateUrl: '/NgApps/Templates/CarFinderDirectiveTemplate.html', | |
//define all functional behavior for this directive | |
link: function(scope, elem, attrs){ | |
scope.years = []; | |
scope.getYears = function (){ | |
carSvc.getYears().then(function(data) { | |
scope.years = data; | |
}); | |
} | |
//get rolling! | |
scope.getYears(); | |
} | |
} | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment