I have written my very first controller and it is so much fun.
I am changing date format from DOM to model and model to DOM with $parses 
and $formatters
This works great but I am having hard to triggering $parsers... 
I am able to trigger $formatters by changing value on model by issuing 
$digest
but I am unable to change value as shown below. Any advice would be 
appreciated. 


my controller:

'use strict';

angular.module('gdClientApp')
    .directive('datFormat', function ($filter) {
        return {
            require: 'ngModel',
            restrict: 'A',
            scope: { ngModel: '=' },
            link: function (scope, element, attrs, ngModel) {
                var fromUser = function (data) {
                    var date = new Date(data);
                    return $filter('date')(date, 'yyyy-MM-dd');
                };
                var toUser = function (data) {
                    if (data === '0001-01-01') {
                        return 'N/A';
                    }else {
                        return $filter('date')(data, 'MM/dd/yyyy');
                    }
                };
                ngModel.$parsers.push(fromUser);
                ngModel.$formatters.unshift(toUser);
            }
        };
    });


my test file:

'use strict';

ddescribe('Directive: datFormat', function () {

    // load the directive's module
    beforeEach(module('gdClientApp'));

    var element,
        scope;

    beforeEach(inject(function ($rootScope, $compile) {
        scope = $rootScope;
        scope.model = {'date': '2012-10-05'};
        element = angular.element('<input type="text" date-format 
ng-model="model.date" id="txt_field">');
        $compile(element)(scope);
        scope.$apply();
    }));

    it('should format data from iso', inject(function () {
        expect(element.val()).toEqual('10/05/2012');
    }));

    it('should populate data as N/A', inject(function () {
        scope.model = {'date': '0001-01-01'};
        scope.$digest();
        expect(element.val()).toEqual('N/A');
    }));

    it('should format date to iso', inject(function () {
        console.log(element.val())
        element.val('10/25/2013')
        scope.$digest();
        expect(scope.model.date).toEqual('2013-10-25'); << FAILED: 
 Expected '2012-10-05' to equal '2013-10-25'.
    }));
});

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to