2014-10-30 6 views
15

ho la seguente direttiva:

!(function (window, angular) { 
    'use strict'; 

    /** 
    * @ngdoc directive 
    * @name app.directive:social 
    * @description 
    * # social 
    */ 
    angular.module('app') 
     .directive('social', function(social_network_conf) { 
      return { 
       restrict: 'A', 
       scope: { 
        social: "@" 
       }, 
       require: 'ngModel', 
       controller: function($scope, $element){ 
        //for tests only 
        $scope.render = function(){ 
         //how to I get the ngModel here 
         ngModel.$render(); 
        }; 

        $scope.setViewValue = function(val){ 
         ngModel.$setViewValue(val); 
        }; 
       }, 
       link: function(scope, element, attr, ngModel) { 

        ngModel.$formatters.push(function(value) {// from model to view 
         value = value.trim(); 
         if(value){ 
          if (value.indexOf(social_network_conf.matcher) === 0){ 
           var split_link = value.split(social_network_conf.divider); 
           return split_link[split_link.length-1]; 
          } 
          else{ 
           return value; 
          } 
         } 
        }); 

        ngModel.$parsers.push(function(value) { // from view to model 
         value = value.trim(); 
         if(value){ 
          if (value.indexOf(social_network_conf.matcher) === 0){ 
           return value; 
          } 
          else{ 
           return social_network_conf.prefix + scope.social + 
             social_network_conf.suffix + value; 
          } 
         } 
        }); 
       } 
      }; 
     }); 
}(window, window.angular)); 

Il test va come segue:

'use strict'; 

describe('Directive: social', function() { 

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

    var element, 
    social_network_conf, 
    linker, 
    scope, 
    $httpBackend; 

    beforeEach(inject(function ($rootScope, _$httpBackend_, _social_network_conf_) { 
    scope = $rootScope.$new(); 
     social_network_conf = _social_network_conf_; 
     //Must be an object to make use of prototypical inheritence for out-side-of-isolate-scope access 
     scope.models = {}; 

     $httpBackend = _$httpBackend_; 
     $httpBackend.whenGET(/views\/social.html/).respond('<div></div>'); 
     $httpBackend.whenGET(/views\/navigation.html/).respond('<div></div>'); 

    })); 

    it('It should convert ngModel into full HTTP address notation', inject(function ($compile) { 
    element = angular.element('<input social="test_network" ng-model="models.test_network"></social>'); 
    linker = $compile(element); 
    element = linker(scope); 
    scope.$apply(function(){ 
     element.val('test'); 
    }); 
    scope.$digest(); 
    expect(scope.models.test_network).toBe(social_network_conf.prefix + 'test' + 
      social_network_conf.suffix); 
// expect(element.text()).toBe('this is the social directive'); 
    })); 
}); 

Il problema è che queste linee:

scope.$apply(function(){ 
    element.val('test'); 
}); 

Non richiamare effettivamente il parser $ definito.

Sebbene crei un controller per la direttiva con un'API per chiamare ngModel.$render o ngModel.$setViewValue ma non ho modo di accedere a ngModel sul controller di direttiva senza un brutto attacco.

risposta

19

2 possibili soluzioni:

primo è quello di avvolgere l'elemento all'interno di un modulo, e per assegnare il campo di input e forma un attributo name, e quindi accedere al campo di input come segue:

scope.form_name.input_name.$setViewValue('value') 

codice lavoro:

'use strict'; 

describe('Directive: social', function() { 

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

    var element, 
    social_network_conf, 
    linker, 
    scope, 
    $compile, 
    $body, 
    html, 
    $httpBackend; 

    beforeEach(inject(function ($rootScope, _$compile_, _$httpBackend_, _social_network_conf_) { 
    scope = $rootScope.$new(); 
     social_network_conf = _social_network_conf_; 
     //Must be an object to make use of prototypical inheritence for out-side-of-isolate-scope access 
     scope.models = {}; 
     $compile = _$compile_; 
     $httpBackend = _$httpBackend_; 
     $body = $('body'); 
     $httpBackend.whenGET(/views\/social.html/).respond('<div></div>'); 
     $httpBackend.whenGET(/views\/navigation.html/).respond('<div></div>'); 
     $body.empty(); 
     html = '<form name="testForm">' + 
       '<input social="test_network" name="test" ng-model="models.test_network">' + 
      '</form>'; 

    })); 

    it('It should convert ngModel into full HTTP address notation', function() { 

    element = angular.element(html); 
    linker = $compile(element); 
    element = linker(scope); 

    var viewValue = 'test', 
     input = element.find('input'); 

    scope.models.test_network = viewValue; 
    scope.$digest(); 

    scope.testForm.test.$setViewValue(viewValue); 

    scope.$digest(); 
    expect(scope.models.test_network).toBe(social_network_conf.prefix + input.isolateScope().social + 
      social_network_conf.suffix + viewValue); 

    }); 

in alternativa, il secondo offre di inviare un evento di ingresso sull'elemento - non ha funzionato per me con angolare 1.3. Ciò è dimostrato in This YouTube video. risposta

element.val('test'); 
element.trigger('input'); 
+1

Sono riuscito a ottenere .val() .trigger() per funzionare. Grazie mucho – welbornio

+1

.val ("Whatever"). Trigger ("input") funziona anche per me! –

+4

.trigger non era disponibile per me. Ho usato .val ("qualunque cosa"). TriggerHandler ("input") invece –

1

Shorter per i pigri: semplicemente innescando change o input sull'elemento ha lavorato per me.

In sostanza:

element.val('test').trigger('change') 
$rootScope.$digest() 

dovrebbe attivare il parser è stato aggiunto.

0
element.trigger('input'); 

ha lavorato per me quando si esegue il test in Chrome, ma in esecuzione in IE fallito.

element.trigger('change'); 

Ha funzionato sia in IE che in Chrome.

Questo potrebbe essere il motivo per cui le persone segnalano risultati diversi.