Archive

Posts Tagged ‘angularjs’

AngularJS CheatSheet

ng-app (usually on html)

ng-controller (on the body or on the container)

‘use strict’;

scripts order: Jquery – Angular (in the head possibly) other scripts: 

var testApp = angular.module(‘myFirstAng’, []);
testApp.controller(‘myController’, function ($scope) {
$scope.Test = “hello world”;
});


 

use of $http:

myApp.controller(‘myController’, function ($scope, $http) {

$http({
dataType: ‘json’, headers: {
“Content-Type”: “application/json”
}, data: ”, method: ‘GET’,
url: ‘/api/Sdb’
}).success(function (data, status, headers, config) {
$scope.Result = data;
})
.error(function (data, status, headers, config) {
}); 
});


 

NG-Repeat: ng-repeat=”friend in friends”

ng-click

<select
data-ng-model=”selection.bg”
data-ng-options=”bg.Name for bg in businessGroups”
data-ng-change=”ChangeEvent()” >
<option value=””>-Select-</option>
</select>


 

ng-grid

 <div class=“gridStyle” ng-grid=“gridOptions” style=“width: 90%; height: 90%”></div>

 

 $scope.gridOptions = {
                data: ‘users’, columnDefs: [
                    { field: ‘FullName’, displayName: ‘FullName’, resizable:true, width:200 },
                    { field: ‘UserName’, displayName: ‘UserName’, resizable: true },
                    {
                        field: ‘LastLogin’, displayName: ‘LastLogin’,
                        cellFilter: “date:’dd/MM/yyyy HH:mm'”
                    },
                    { field: ‘AssignedRoles’, displayName: ‘AssignedRoles’, resizable: true, cellFilter: ‘stringArrayFilter’ }]
            };
 
getUsersService.getUsers(data).then(
                            function (dataReturned) {
                                $scope.users = dataReturned;
                            });
 

Jasmine testing:
describe("myApp", function() {
 
    beforeEach(module('myApp'));
 
    describe("SimpleController", function() {
 
        var scope;
        beforeEach(inject(function($rootScope, $controller) {
            scope = $rootScope.$new();
            $controller("SimpleController", {
                $scope: scope
            });
        }));
 
        it("should double the numbers", function() {
            scope.doubleIt();
            expect(scope.x).toBe(6);
        });
    });
});
 
Advertisements
Categories: DEV, Test Tags: ,