on
Italy
- Get link
- X
- Other Apps
ngRoute module. Although this is
good enough for basic routes, it doesn’t support more complex scenarios,
such as nested views, parallel views or a sequence of views. ngRoute couldn’t.ngRoute was not created with complex enterprise
applications in mind. I have personally seen applications where certain
portions of a page need to be loaded in several steps. Such
applications can be built using ngRoute, but it is almost impossible to have a URL state for every single change applied to the view.ng-view directive can be used only once inside an instance of the ng-app directive. This prevents us from creating parallel routes, as we cannot have two parallel views loading at the same time.ng-view cannot contain another ng-view directive. This prevents us from creating nested views.Controller as syntax . I highly recommend using the Controller as syntax, as it is one of the conventions to be followed today to get ready for Angular 2.path: URL of the route’s templatecomponent: a combination of a template and a controller. By convention, both controller and template have to be named after the component$router service . As $router
is a service, we can define the routes anywhere in the app (other than
in a provider or, config block). However, we need to make sure that the
block of code defining routes is executed as soon as the app is loaded.
For example, if the routes are defined in a controller (as we will do
shortly), the controller has to be executed on page load. If they are
defined in a service, the service method has to be executed in a run
block.angular.module('simpleRouterDemo', ['ngNewRouter'])
.controller('RouteController', ['$router', function($router){
$router.config([
{ path:'/', redirectTo:'/first' },
{ path:'/first', component:'first' },
{ path:'/second/:name', component:'second' }
]);
this.name='visitor';
}])
ngRoute.firstController and secondController in
our case). The name of the view template has to be the same as the name
of the component. It also has to reside in a folder with the same name
as the component, inside a folder named components. This would give us:projectRoot/
components/
first/
first.html
second/
second.html
These conventions can be overridden using $componentLoaderProvider. We will see an example of that later, but for now let’s stick to the conventions.first and second used above. We’re defining them in-line using the ng-template directive (so that we can recreate a runnable demo), but ideally they should be in separate HTML files:
angular.module('simpleRouterDemo')
.controller('FirstController', function(){
console.log('FirstController loaded');
this.message = 'This is the first controller! You are in the first view.';
})
.controller('SecondController', function($routeParams){
console.log('SecondController loaded');
this.message = 'Hey ' + $routeParams.name +
', you are now in the second view!';
});
Controller as syntax, they don’t accept $scope. The $routeParams service is used to retrieve the values of the parameters passed in the route.
ng-link directive and ng-viewport directive, which link views and load templates respectively. The ng-viewport directive is similar to ng-view; it’s a placeholder for part of your app loaded dynamically based on the route configuration.
ng-viewport can be used any
number of times inside an application. Consequently, it is possible to
define multiple parallel views on a page. The viewports have to have
unique identifiers, so as to load components into them through the route
definition.
Imagine we wanted to place this code into a folder called parallel and the view templates inside parallel/components. This would give us:projectRoot/
parallel/
components/
first/
first.html
second/
second.html
components
folder in the project root), we need to tell the router to look for the
views in a new folder. The following config block does this:angular.module('parallelRouterDemo', ['ngNewRouter'])
.config(['$componentLoaderProvider', function($componentLoaderProvider){
$componentLoaderProvider.setTemplateMapping(function (name) {
return 'parallel/components/' + name + '/' + name + '.html';
});
}])
Routes for this page have to load two components and display them in
the different viewports. The configuration object uses the viewports’
unique identifiers to specify which view template is rendered where.$router.config([
{
path: '/:name', component: {
left: 'first',
right: 'second'
}
},
{
path: '/swap/:name', component: {
left: 'second',
right: 'first'
}
},
{
path: '/',
redirectTo: '/there'
}
])
true or a resolved promise would pass through the lifecycle event and a Boolean false or a rejected promise would cancel further operation.canReactivate: Indicates if a view can be re-activated. It
can be used to persist the state of the view and optimize the loading
time of the view upon subsequent requests.canActivate: Runs before activating a component. Activates the component when a resolved promise or true is returned and cancels otherwise.canDeactivate: Runs before deactivating a component. Unloads the component when a resolved promise or true is returned and cancels otherwise.$scope to detect a lifecycle event.canActivate method works by means of an an example. This method can be used to check if a user can access a view before loading it. this.canActivate = function(){
var hasAccess = userAccessInfo.hasAccessToSecondComponent;
if(!hasAccess){
$window.alert('You don\'t have access to this view.
Redirecting to previous view ...');
}
return hasAccess;
};
canDeactivate method. This could be used to restrict a user from navigating away from a page with unsaved changes. this.canDeactivate = function () {
if (this.sampleText) {
var alertResult = $window.confirm('You have unsaved changes.
Do you want to leave the page?');
return alertResult;
}
return true;
};
Comments
Post a Comment