ng-model is not updating when using jquery element.val()

后端 未结 4 1341
天命终不由人
天命终不由人 2020-12-18 09:09

PLUNKR example here

I\'m using some version of jquery autocomplete as an angularjs direcitve. When the jquery updates the input\'s value using element.val()

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-18 09:54

    The plugin you're using has an onSelect callback, so you can simply modify your autocomplete parameters to include a callback which updates the scope

    {
        lookup      : $scope.current,
        width       : 448,
        delimiter   : /,/,
        tabDisabled : true,
        minChars    : 1,
        onSelect: function(v, data) {
            $scope.selected_tags = v.value;
            $scope.$apply();
    }
    

    EDIT

    To further improve this you can remove the need to use the model name in the callback:

    function(v, data) {
        var model = $(this).attr("ng-model");
        $scope[model] = v.value;
        $scope.$apply();
    }
    

提交回复
热议问题