Showing posts with label angularjs. Show all posts
Showing posts with label angularjs. Show all posts

Tuesday, April 4, 2017

how to use const vars in AngularJS

1. define const with .constant

 angular.module('myapp').constant("CONST", {
     "URL" : "www.google.com",
     "WELCOME_MSG": "Hello world",
     "PI" : 3.1415926
  }); 

2. Reference consts with:
angular.module('myapp').controller('myCtrl', ['$scope', '$rootScope', '$log', 'CONST', function($scope, $rootScope, $log, CONST) {
     $log.info("URL is", CONST.URL);
 }]);

Monday, March 6, 2017

$http to call REST APIs in Angularjs

Examples:
1. GET
            var successCallback = function(response) {
                $scope.kerberos = response.data.Kerberos;
                if ($scope.kerberos == "disabled") {
                    $log.info('kerberos is disabled.');
                } else {
                    $log.info('kerberos is enbaled.');
                }
            };

            var errorCallback = function(response) {
                $scope.error = response.data;
            };

            $http({
                method: 'GET',
                url: '/api/v1.0/configuration/kerberos'
            })
                .then(successCallback, errorCallback);

2. POST
            $scope.addDevice = function($event) {
                if($scope.addDevice.password != $scope.addDevice.confirmPassword) {
                    alert("Passwords Don't Match");
                    return false;
                }

                var successCallback4addDevice = function(response) {
                    $log.info('Device', response.data.id);
                    eventBus.emit('backup:adddevice:off', response);
                };

                var errorCallback4addDevice = function(response) {
                    eventBus.emit('backup:adddevice:off', response);
                };

                var dataobj = {
                        username: $scope.addDevice.user,
                        hostname: $scope.addDevice.server,
                        path: $scope.addDevice.storage
                };
                if ($scope.kerberos == "disabled") {
                    $log.info('create password obj');
                    dataobj.password = $scope.addDevice.password;
                } else {
                    $log.info('create kerberoscc obj');
                    dataobj.kerberoscc = $scope.addDevice.kerberoscc;
                }

                eventBus.emit('backup:adddevice:on');
                $http({
                    method: 'POST',
                    url: '/api/v1.0/configuration/backup-devices',
                    data: dataobj
                })
                    .then(successCallback4addDevice, errorCallback4addDevice);

            };

Friday, February 24, 2017

angularjs - access value of ng-model in a controller

To access the value of ng-model in a controller, you have to have a dot in there.
With a format like object.property you'll be good to go.

In my case "add.device.storage" does not work. And  "addDevice.storage" is good.

In view(.html):

<input type="text" id="input-id-dd-password" ng-model="addDevice.password"
    placeholder="type here">

In controller(.js): access it with $scope.addDevice.password