Here's a fun one...
We all have need for show/hide divs every now and again, right? Why not let Angular do the heavy lifting?
If you haven't played with directives yet, you should start learning. They're powerful!
Here's the fiddle. This directive looks for and divs with a 'class="showhide"' and then makes them collapsible divs. (Click on "result" above to see how they work.)
Within each "showhide" div, we create a"less" div, a "more" div, and a "show" div. Less is the text that will show when the div is collapsed, More shows when it's open, and the Show div has the link to turn them on and off.
I think that the code is pretty self-explanatory, but if you have any questions, hit us up in the comments.
Tuesday, September 17, 2013
Thursday, September 12, 2013
Custom Angular Filters
The current filter docs are a bit confusing. Let's break down how simple it is to add an Angular filter!
The currency filter does not allow you to choose how many decimals are involved. So let's create a custom filter that does currency in whole dollars.
You can now call this filter like so:
So, let's breakdown the code so we know exactly what we need to do.
We are creating an angular module.
We name the module 'dollarFilter'. The array brackets would be where you would list any dependent modules. So let's look at the controller init:
dollarFilter is now a required module for our controller and will be loaded on start up.
Note that dollarFilter is the name of the module itself, NOT the name of our filter! This name is only used for the injector. Modules can contain multiple filters, coding, etc...
Here is where we actually name our filter. In this case, "dollars". That is the name we use when we want to call the filter.
Now we write the function. In the case of a filter, it will take one argument representing the value passed to our filter.
I've set up a basic Fiddle for you to play with and see how the code works. (You can also get the "addCommas" function from here.)
What if we want to add multiple filters to one module?
Declare our filter, and store the object in a variable.
Add filters to the object:
And here's another fiddle for you to see how it comes together.
Hope this helps. If there's a question we didn't answer, let me know in the comments.
The currency filter does not allow you to choose how many decimals are involved. So let's create a custom filter that does currency in whole dollars.
var app = angular.module('app', ['dollarFilter']); angular.module('dollarFilter', []).filter('dollars', function() { return function(value) {return "$" + addCommas(Math.round(value));} });
You can now call this filter like so:
{{total | dollars}}
So, let's breakdown the code so we know exactly what we need to do.
We are creating an angular module.
angular.module('dollarFilter', []
We name the module 'dollarFilter'. The array brackets would be where you would list any dependent modules. So let's look at the controller init:
var app = angular.module('app', ['dollarFilter']);
dollarFilter is now a required module for our controller and will be loaded on start up.
Note that dollarFilter is the name of the module itself, NOT the name of our filter! This name is only used for the injector. Modules can contain multiple filters, coding, etc...
.filter('dollars', function() {
Here is where we actually name our filter. In this case, "dollars". That is the name we use when we want to call the filter.
Now we write the function. In the case of a filter, it will take one argument representing the value passed to our filter.
return function(value) {return "$" + addCommas(Math.round(value));}
I've set up a basic Fiddle for you to play with and see how the code works. (You can also get the "addCommas" function from here.)
What if we want to add multiple filters to one module?
Declare our filter, and store the object in a variable.
var df = angular.module('dollarFilter', []);
Add filters to the object:
df.filter('dollars', function() { return function(value) {return "$" + addCommas(Math.round(value));} }); df.filter('percentage', function () { return function(value) {return value * 100 + "%";} });
And here's another fiddle for you to see how it comes together.
Hope this helps. If there's a question we didn't answer, let me know in the comments.
Wednesday, September 11, 2013
Passing your PHP variables to Angular
New to angular from other JS frameworks and can't figure out how to pass PHP variables to Angular? I hit this when I first started, and Google was of no help.
In General, you want to use AJAX calls to get values from your database to your angular app. But what about user authentication?
That's a pretty common piece of code. So, how do you pass the username to your Angular app?
Use ng-init when you set up your controller in the HTML. Use PHP to echo variables to the page.
Now, create a userInit function in your angular controller that will handle the variables:
And it's that simple!
In General, you want to use AJAX calls to get values from your database to your angular app. But what about user authentication?
<?php session_start(); if (empty($_SESSION['uname'])){ header("location:auth/login.php"); } ?>
That's a pretty common piece of code. So, how do you pass the username to your Angular app?
Use ng-init when you set up your controller in the HTML. Use PHP to echo variables to the page.
<div class="container-fluid" ng-controller="appCtrl" ng-init="userInit('<?php echo $_SESSION['uname']."', '".$_SESSION['role'] ?>')" >
Now, create a userInit function in your angular controller that will handle the variables:
$scope.userInit = function(uid, role) { $scope.user = uid; $scope.role = role; // Get a list of projects for user $scope.projectList($scope.user); }
And it's that simple!
Subscribe to:
Posts (Atom)