}
);
};
+ vm.getCar = function() {
+
+ console.log('getCar: START');
+
+ cars.getById(1).then(
+ function (value) {
+ console.log('getCar getById: Cars controller value: ' + JSON.stringify(value));
+ },
+ function(reason) {
+ console.log('getCar getById: Cars controller reason: ' + JSON.stringify(reason));
+ }
+ );
+
+ cars.getExpectedValue().then(
+ function (value) {
+ console.log('getCar getExpectedValue: Cars controller value: ' + JSON.stringify(value));
+ },
+ function(reason) {
+ console.log('getCar getExpectedValue: Cars controller reason: ' + JSON.stringify(reason));
+ }
+ );
+
+ console.log('getCar: END');
+ };
+ vm.getResolvedPromise = function() {
+ console.log('getResolvedPromise: START');
+
+ cars.getExpectedValue().then(
+ function (value) {
+ console.log('getResolvedPromise getExpectedValue: Cars controller value: ' + JSON.stringify(value));
+ },
+ function(reason) {
+ console.log('getResolvedPromise getExpectedValue: Cars controller reason: ' + JSON.stringify(reason));
+ }
+ );
+
+ console.log('getResolvedPromise: END');
+ };
// How to test "private" methods in controllers?
// Two options:
ng-click="vm.getCars()">
Get cars
</button>
+ <button type="button" class="btn btn-primary" ng-disabled="myForm.input.$invalid"
+ ng-click="vm.getCar()">
+ Get car
+ </button>
+ <button type="button" class="btn btn-primary" ng-disabled="myForm.input.$invalid"
+ ng-click="vm.getResolvedPromise()">
+ Get resolved promise
+ </button>
</form>
<label>controller as directive:</label>
*/
/* @ngInject */
function cars($http, $q, API) {
+ var expectedValue;
+
return {
- getAll: getAll
+ getAll: getAll,
+ getById: getById,
+ getExpectedValue: getExpectedValue
};
/**
console.log('XHR Finalizer for getAll.');
}
}
+
+ /**
+ * @ngdoc method
+ * @name getById
+ * @methodOf app.cars.cars
+ *
+ * @description
+ * Get one car by its id from API REST.
+ */
+ function getById(id) {
+ expectedValue = $http.get(API.CAR.replace(':carId', id));
+
+ return expectedValue;
+ }
+
+ function getExpectedValue() {
+ return expectedValue;
+ }
}
}());