javascript - Why doesn't my ng-model update with Typeahead in Firefox? -
in website i'm building angularjs i've got input typeahead below use angular-typeahead. works fine in chrome, opera , safari not in firefox. problem seems in firefox model isn't updating when click typeahead suggestion.
my html looks this:
<input class="typeahead" sf-typeahead type="text" datasets="userdataset" ng-model="searchuser" > <button ng-click="sendtouser(searchuser.id)" class="btn">send</button>
and in controller have simple function:
$scope.sendtouser = function(userid){ console.log(userid); // more code here.. }
in chrome, opera , safari logs int userid
, in firefox logs undefined
.
i made plunker here show mean (search "one", or "two").
it works in chrome, opera , safari, in firefox somehow shows undefined
in console. weird thing shows undefined
first time. if select second time work.
does know why doesn't work in firefox, , importantly, how can solve it? tips welcome!
this interesting dilemma.
the typeahead sets
ng-model
value object once selectedupon event (like click) runs digest cycle, framework enforces
ng-model
binding,assigning model value string bound input.firefox, unlike chrome seems enforce behavior. chrome inputs allow object value setting (just guess).
the work around change output binding:
<input class="typeahead" sf-typeahead type="text" datasets="userdataset" outputval="searchuser" ng-model="a" options="exampleoptions">
the outputval
our expected value. bound random scope variable a
because directive expects , uses model binding.
inside directive, changed updatescope
function set selected value scope.outputval
, commented out assignment model.
function updatescope (object, suggestion, dataset) { scope.$apply(function () { var newviewvalue = (angular.isdefined(scope.suggestionkey)) ? suggestion[scope.suggestionkey] : suggestion; console.log(newviewvalue); //ngmodel.$setviewvalue(newviewvalue); scope.outputval = newviewvalue; }); }
try plunker!
Comments
Post a Comment