Skip to content

Instantly share code, notes, and snippets.

@steppefox
Last active December 29, 2015 08:08
Show Gist options
  • Save steppefox/7640956 to your computer and use it in GitHub Desktop.
Save steppefox/7640956 to your computer and use it in GitHub Desktop.
JS Angular snippets

**** AngularJS Foreach ****

angular.forEach(values, function(value, key){
  this.push(key + ': ' + value);
}, log);

**** AngularJS Ng-cloak usage ****

<div id="template1" ng-cloak>{{ 'hello' }}</div>
<div id="template2" ng-cloak class="ng-cloak">{{ 'hello IE7' }}</div>

**** AngularJS filter for price presentation devided by 3 numbers ****

angular.module('cartFilters', [])
.filter('nicePrice',function(){
    return function(input){
        price = parseInt(input);
        var w = price.toString().split('').reverse();
        var w2=[];
        for(i in w){
            if(i%3==0)
                w2.push(' ');
            w2.push(w[i]);
        }
        delete w;
        return w2.reverse().join('');
    };
});
// or you can use
'1234567890'.replace(/\B(?=(\d{3})+(?!\d))/g,' ');

**** AngularJS ngSwitch structure ****

<div ng-switch on="selection" >
    <div ng-switch-when="settings">Settings Div</div>
    <span ng-switch-when="home">Home Span</span>
    <span ng-switch-default>default</span>
</div>

**** AngularJS watch structure ****

$scope.$watch('[filters.size_width, filters.size_height, filters.size_d, filters.region]|json', function() {
    refreshList('filt');
})

**** AngularJS ng-class structure ****

<div data-ng-class="{true:('big'+num),false:'dot2'}[num!='.']"></div>
<div data-ng-class="{'cl1':1==1,'cl2':1==2}"></div>

**** AngularJS checkbox trick ****

<input type="checkbox"  data-ng-model="item.checked" data-ng-true-value="1" data-ng-false-value="0" data-ng-checked="item.checked==1">

AngularJS ngSelect

<select data-ng-model="blah" data-ng-options="item.id as item.title for item in items"></select>
<input type="text" style="display:none;" data-ng-model="blah"/>

**** Dirty tricks ****

var model = angular.element($('#someElement')).scope().model;
<a ng-click="actions.someAction($event)">click</a>

**** Custom brackets in Angular ****

var m = angular.module('myApp', []);
m.config(function($interpolateProvider) {
  $interpolateProvider.startSymbol('(('); 
  $interpolateProvider.endSymbol('))'); 
});

**** PHP $_POST input ****

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment