together
- Create a value service named "NameVal"
- Give it a string value of your name
- Inject it into both controllers
- Output it in the template under both controllers
on your own
- Add an input field
- Give it an ng-model with the same variable name as the variable you're using for the names
- Note what happens when you change one
together
- Change the value to an object
- Assign the object directly to a variable on the controller
- Use that object
- and make sure you don't assign a primitive to the $scoped controller
- assign the entire object
- "there should always be a dot in your model"
on your own
- Create a "Users" value service
- Add the array included in
names.json
to it - Create an ng-repeat of all of the items in the users value
- Output the name item in a list
on your own
- Create a new file
users.js
- Move the
Users
service to that file
- it will have to read:
angular.module('serviceapp').value(...
- Note the difference between the initialization of the application module and this
together
- Create a factory that uses the users value
- Give the factory one function to remove a user
- Add this one to ControllerOne
together
- Create a service that will do the same exact thing as the factory
- Add this one to ControllerTwo
mostly together
- Register the app with angular.mocks
beforeEach(module('serviceapp'));
- Inject the
$injector
- Use the
$injector
toget()
the UserFact
userFact = $injector.get('UserFact');
- Do the same for
Users
- Make sure there are variables for
userFact
andusers
that are available in theUserFact
's describe scope - Write a test that checks
users
's length and then removes the 0th element and then checks to make sure the length is one less than it was
together
- Use
angular.mocks.module
to include$provide
- Use
$provide
to create avalue
called "Users" - Give it some dummy values
module(function($provide){
$provide.value('Users', [
{name: "one"},
{name: "two"}
]);
});
on your own
- Do this, it's no different from the Factory test
together
- Create a new service called "UserHttp"
- Inject the $http service into our new service
- Declare an empty array on the service
- Attach the following function onto it:
svc.getUsers = function(){
$http.get('/users.json')
.then(function(response){
svc.users = response.data;
});
together
- Inject the new service into the controller (instead of the
Users
service) - Attach the new service's users variable to a controller variable
- Try it
- Attach the new service to a new service variable in the controller
- something like
c1.usersHttp = UsersHttp;
- Change the template to reflect where the data is coming from
together
- Inject $injector beforeEach
- Get $httpBackend with:
$httpBackend = $injector.get('$httpBackend');
- Use $httpBackend to reply with a 200 and some users whenever the URL we want to hit is accessed
$httpBackend
.when('GET', '/users.json')
.respond(200, [{name:"one"}, {name:"two"}]);
- Make sure you also $injector.get the
UsersHttp
service - Test the returned function from the service
- It should return some users
- Add a callback function to the getUsers function
- when it's in the app, we don't currently need it to do anything
- when it's in the test, we'll need to use the callback to run our expect after it returns
on your own
- Just change
/users.json
tohttp://jsonplaceholder.typicode.com/users
- that's fun, right?
- Remember to change it in the test as well