2014-10-30 38 views
15

我有以下指令:

!(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)); 

测试云如下:

'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'); 
    })); 
}); 

问题是,这些行:

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

实际上不要调用我定义的$ parser。

虽然创建一个API的指令控制器调用ngModel.$renderngModel.$setViewValue但我没有办法访问ngModel在指令控制器没有一个丑陋的黑客。

回答

19

2个可能的解决方案:

首先是包裹元件的形式的内部,并将所述输入字段和形式的name属性分配,然后进入输入字段如下:

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

工作代码:

'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); 

    }); 

另外,第二个提供的元素分派输入事件 - 我与角1.3没有工作。 这在This YouTube video中得到证明。对于懒人

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

我能够.val().trigger()工作。谢谢mucho – welbornio 2015-02-16 19:01:10

+1

.val(“whatever”)。trigger(“input”)也适用于我! – 2015-04-01 03:14:49

+4

.trigger不适用于我。我用.val(“无论”)。triggerHandler(“输入”) – 2015-06-11 23:39:49

1

短的答案:简单地触发元素上changeinput为我工作。

基本上是:

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

应触发您添加的解析器。

0
element.trigger('input'); 

运行时,Chrome的测试,但在IE中运行它没有为我工作。

element.trigger('change'); 

曾在IE和Chrome中工作。

这可能是人们报告不同结果的原因。