Exchange Data Between Directive and Controller in AngularJS

  • The project required me to write a directive for a canvas, get the Base64 image data when a user draws on it, and make it available to the controller of the page which contains the directive.
  • This boils down to the fact that you need to access the scope of a Directive from a page Controller and access the scope of a page Controller from a Directive.
  • Now let’s say we have the following controller:

    And the following template:

    The problem: we would like to bind the count variable of myCtrl to the internalCount variable of the counter directive.

  • This can easily be solved by binding a variable from the controller to the scope of the directive.
  • Your directive should now look like this:

    And your template like this:

    You will find a working fiddle here with all the code to this quick tutorial on how to exchange data between a directive and controller in AngularJS.

In this Quick Tutorial we will look at How to Exchange Data Between Directive and Controller in AngularJS and bind the scopes to each other.
Continue reading “Exchange Data Between Directive and Controller in AngularJS”

Exchange Data Between Directive and Controller in AngularJS

  • The project required me to write a directive for a canvas, get the Base64 image data when a user draws on it, and make it available to the controller of the page which contains the directive.
  • This boils down to the fact that you need to access the scope of a Directive from a page Controller and access the scope of a page Controller from a Directive.
  • Now let’s say we have the following controller:

    And the following template:

    The problem: we would like to bind the count variable of myCtrl to the internalCount variable of the counter directive.

  • This can easily be solved by binding a variable from the controller to the scope of the directive.
  • Your directive should now look like this:

    And your template like this:

    You will find a working fiddle here with all the code to this quick tutorial on how to exchange data between a directive and controller in AngularJS.

In this Quick Tutorial we will look at How to Exchange Data Between Directive and Controller in AngularJS and bind the scopes to each other.
Continue reading “Exchange Data Between Directive and Controller in AngularJS”

Exchange Data Between Directive and Controller in AngularJS

  • The project required me to write a directive for a canvas, get the Base64 image data when a user draws on it, and make it available to the controller of the page which contains the directive.
  • This boils down to the fact that you need to access the scope of a Directive from a page Controller and access the scope of a page Controller from a Directive.
  • Now let’s say we have the following controller:

    And the following template:

    The problem: we would like to bind the count variable of myCtrl to the internalCount variable of the counter directive.

  • This can easily be solved by binding a variable from the controller to the scope of the directive.
  • Your directive should now look like this:

    And your template like this:

    You will find a working fiddle here with all the code to this quick tutorial on how to exchange data between a directive and controller in AngularJS.

In this Quick Tutorial we will look at How to Exchange Data Between Directive and Controller in AngularJS and bind the scopes to each other.
Continue reading “Exchange Data Between Directive and Controller in AngularJS”

Exchange Data Between Directive and Controller in AngularJS

  • The project required me to write a directive for a canvas, get the Base64 image data when a user draws on it, and make it available to the controller of the page which contains the directive.
  • This boils down to the fact that you need to access the scope of a Directive from a page Controller and access the scope of a page Controller from a Directive.
  • Now let’s say we have the following controller:

    And the following template:

    The problem: we would like to bind the count variable of myCtrl to the internalCount variable of the counter directive.

  • This can easily be solved by binding a variable from the controller to the scope of the directive.
  • Your directive should now look like this:

    And your template like this:

    You will find a working fiddle here with all the code to this quick tutorial on how to exchange data between a directive and controller in AngularJS.

In this Quick Tutorial we will look at How to Exchange Data Between Directive and Controller in AngularJS and bind the scopes to each other.
Continue reading “Exchange Data Between Directive and Controller in AngularJS”

Angular or React: a complete guide

  • In this series of articles we’ll highlight pros and cons of both Angular and React as a solution for your next app.
  • Backbone, Marionette, Ember, Knockout, CanJS, Angular, React… Each of these guys has their pros and cons.
  • Choosing the most suitable for your current task is complicated because you need to consider:

    Let’s take a look at rating of most popular frameworks on Github:

    Discussions used to be about Angular vs Backbone (and most of the time, Marionette along with it).

  • Backbone has proven itself as a great choice for single page applications with a lot of custom logic and performance concerns due to its minimalistic design and its ability to integrate many 3rd party components with ease.
  • React has problems as well, and it positions itself as a library, not a framework, which means you won’t have the tools for covering all of the aspects of a single page app right out of the box, as you would with Angular.

Angular or React: a complete guide
In this series of articles we’ll highlight pros and cons of both Angular and React as a solution for your next app.
Continue reading “Angular or React: a complete guide”

Learning AngularJS Basics

  • Its job is to define the application and serve as a container for all the controllers of that application.To create a module, let’s look at the following code:scriptvar app = angular.module(“appModule”, []);/scriptThis is the syntax to create an AngularJS module.
  • To achieve this, let’s enclose our HTML application in the appModule parameter by using the following code:scriptvar app = angular.module(“appModule”, []);/scriptbody ng-app=”appModule” …/bodyAs you can see, I’ve used the ng-app directive to enclose the body/body of my HTML with the myModule that we created earlier in our script/script tag.Our Module is now ready to use!
  • Let’s start creating a controller by understanding the following code:scriptvar app = angular.module(“appModule”, function($scope) { // Code inside the controller };/scriptLet’s understand the above syntax.The code app.controller means that our controller belongs to the app module that we created earlier.
  • That can be done using the ng-controller directive.scriptvar app = angular.module(“appModule”, function($scope) { // Code inside the controller };/scriptbody ng-app=”appModule” ng-controller=”appController” …/bodyOur controller is now controlling this body … /body tag.Example | AngularJS Module and ControllerProblem statement: We have to create a Module named appModule.
  • Don’t worry, I’ve explained this exact example in the next lesson.Solution: Here’s the solution to this problem statement, I encourage you to try it yourself before looking at the solution:scriptvar app = angular.module(“appModule”, function($scope) { $scope.name = “Fahad A. Chaudhry”; };/scriptbody ng-app=”appModule” ng-controller=”appController” {{ name }}/bodyConclusionIn this article, we tried to cover the following areas:AngularJS ModuleAngularJS ControllerIn the next ArticleIn the next article, we’ll try to understand the following topics:AngularJS Scopes

In out previous lesson we discussed how to add AngularJS in to your HTML page, what are some basic AngularJS attributes, what are Directives, Expressions and what’s the difference between them. A…
Continue reading “Learning AngularJS Basics”

Exchange Data Between Directive and Controller in AngularJS

  • The project required me to write a directive for a canvas, get the Base64 image data when a user draws on it, and make it available to the controller of the page which contains the directive.
  • This boils down to the fact that you need to access the scope of a Directive from a page Controller and access the scope of a page Controller from a Directive.
  • Now let’s say we have the following controller:

    And the following template:

    The problem: we would like to bind the count variable of myCtrl to the internalCount variable of the counter directive.

  • This can easily be solved by binding a variable from the controller to the scope of the directive.
  • Your directive should now look like this:

    And your template like this:

    You will find a working fiddle here with all the code to this quick tutorial on how to exchange data between a directive and controller in AngularJS.

In this Quick Tutorial we will look at How to Exchange Data Between Directive and Controller in AngularJS and bind the scopes to each other.
Continue reading “Exchange Data Between Directive and Controller in AngularJS”