Giter Site home page Giter Site logo

ricky52529 / angular-ui-router-styles Goto Github PK

View Code? Open in Web Editor NEW

This project forked from manuelmazzuola/angular-ui-router-styles

0.0 1.0 0.0 187 KB

This is a simple module for AngularJS that provides the ability to have route-specific CSS stylesheets, by integrating with Angular uiRouter

License: MIT License

JavaScript 100.00%

angular-ui-router-styles's Introduction

angular-ui-router-styles

This is a simple module for AngularJS that provides the ability to have route-specific CSS stylesheets, by integrating with Angular uiRouter.

What does it do?

It allows you to declare partial-specific or route-specific styles for your app using Angular's ui-router $stateProvider service. This solves that problem by allowing you to do something like this:

app.config(['$stateProvider', function($stateProvider){
    $stateProvider

      .state('state1', {
        url: '/state1',
        controller: 'State1controller',
        template: '<div ui-view></div>',
        data: {
          css: 'styles/custom-state1-override.css'
        }
      })

      .state('state1.state12', {
        url: '/:id',
        controller: 'State12Controller',
        templateUrl: 'views/my-template.html'
      })

      .state('state2', {
        url: '/state2',
        controller: 'State2Controller',
        templateUrl: 'views/another-template.html',
        data: {
          css: ['styles/custom-state2-override.css', 'another.css']
        }
      })
        // more states can be declared here
}]);

Note that state1.state12 will have the parent file styles/custom-state1-override.css injected; redefine the css array for override it.

How to install:

  • Install it with Bower via bower install angular-ui-router-styles --save

  • Ensure that your application module specifies uiRouterStyles as a dependency: angular.module('myApplication', ['uiRouterStyles'])

  • Add css file(s) relative path to the state data object

.state('state1', {
  url: '/state',
  controller: 'StateCtrl',
  templateUrl: 'views/my-template.html',
  data: {
    css: 'styles/some-overrides.css'
  }
})

Things to notice:

  • Specifying a css property on the route is completely optional. If the state doesn't have a css property, the service will simply do nothing for that route.
  • You can even have multiple page-specific stylesheets per state, where the css property is an array of relative paths to the stylesheets needed for that route.
  • If a parent state exists the data object is inherited.

This directive does the following things:

  • It compiles (using $compile) an html string that creates a set of tags for every item in the data.css state property using ng-repeat and ng-href.
  • It appends that compiled set of <link /> elements to the <head> tag.
  • It then uses the $rootScope to listen for '$stateChangeStart' events. For every '$stateChangeStart' event, it cleans all css appended before and adds the new css file(s) to the <head> tag if there are any.

angular-ui-router-styles's People

Contributors

nategraves avatar tennisgent avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.