How to trigger ng-change in directive test in AngularJS

后端 未结 5 1053
情歌与酒
情歌与酒 2021-01-01 15:13

I have the following AngularJS directive that creates an input element. Input has ng-change attribute that runs doIt() function. In my

5条回答
  •  梦毁少年i
    2021-01-01 15:56

    I was looking for this simple line for long hours. Just to save that in here.

    How to select value from html-select, using Karma, and so get ng-change function working?

    HTML:

    Controller or directive JS:

      $scope.itemTypes = [{name: 'Some name 1', value: 'value_1'}, {name: 'Some name 2', value: 'value_2'}]
    
      $scope.itemTypeSelected = function () {
        console.log("Yesssa !!!!");
      };
    

    Karma test fragment:

      angular.element(element.find("#selectedItemType")[0]).val('value_1').change();
      console.log("selected model.selectedItemType", element.isolateScope().model.selectedItemType);
    

    Console:

    'Yesssa !!!!'
    'selected model.selectedItemType', 'value_1'
    

提交回复
热议问题