Coder Social home page Coder Social logo

mini-timedirective's Introduction

mini-timeDirective

##Objectives The purpose of this Mini Project is to get you used to creating custom Directives. Custom directives are one of the most powerful aspects of Angular because they allow you to create re-usable components.

###Step 1: Angular Skeleton

  • Fork this repo, then clone your fork.
  • Create the basics of your Angular application. Your file structure should look like this
  mini-time-directive
    index.html
    js
      app.js
      mainCtrl.js
      timeDirective.js

Remember to include ng-app in your html and call your module 'timeApp'. Also, remember to include the Angular CDN as a script in your HTML along with app.js, mainCtrl.js, and timeDirective.js. Go ahead and create your 'timeApp' module in your app.js file.

###Step 2: Add your name to $scope

  • Head over to your mainCtrl.js file and create a controller called mainCtrl then add a property on $scope called name and set it equal to your name.
  • Head over to your index.html and add 'Hello, ' + whatever the name property on your controller is.

###Step 3: Directive Time

  • Below where it says 'Hello, ' + your $scope.name variable, add the following directive <show-time></show-time>
  • This directive is going to display 'The current time is ' + whatever the current time is. Although this is a small example, think of the bigger picture here. Now we can throw in this <show-time></show-time> directive anywhere in our application without having to recreate code.
  • Now it's time to actually build the directive. Double check that you're not getting any errors in your console before we move on.
  • Head over to timeDirective.js
  • Go ahead and get your module, setting it equals to a variable called 'app'
  • Now, add a directive property onto your app giving it a string, which represents the name of your directive, call it 'showTime' as the first argument. The second argument is a callback function that will return an object. The skeleton of your directive should look something like this.
  angular.module('timeApp').directive('showTime', function(){
  return {

  }
});
  • Now what we want to do is give our directive certain properties. The first one is we want to make sure it's only used as an element, i.e., <show-time>. You do this by setting restrict: 'E' in the object that's being returned from the directives callback.
  • The next property we want to give it is a template. This template is just an HTML string that will show wherever the directive is used. Add a property to the object being returned called 'template' whose value is <div> The current time is {{time}} </div>
  • What we're going to do now is use the link method to get the current time, then update scope.time which will in turn update the template.
  • Add a method onto your returned object called link. Remember, this link method is where you want to put all your DOM manipulation for JavaScript.
  • The link value usually takes three parameters. scope, element, and attrs. Add those as parameters now
  • Now create a variable called currentTime and set it equal to new Date(), which is just the current time.
  • Now, add a property on scope called time which is equal to currentTime.toString
  • If you did everything right reload your index.html page and you should see something like
  Hello, Tyler
  The current time is Sun Dec 07 2014 22:03:12 GMT-0700 (MST)
  • If you don't see that, check your console and start debugging.
  • Your final timeDirective.js file should look like this
angular.module('timeApp').directive('showTime', function(){
  return {
    restrict: 'E',
    template: '<div> The current time is {{time}} </div>',
    link: function(scope, element, attrs){
      var currentTime = new Date();
      scope.time = currentTime.toString();
    }
  }
});

Copyright

© DevMountain LLC, 2016. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.

mini-timedirective's People

Contributors

hathawayjess avatar jrobber avatar kelsymichael avatar r-walsh avatar tylermcginnis 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.