angular.js binding jquery autocomplete object to ng-model, input value to property of object

Problem

I am using in my application angular.js and jquery autocomplete. So, I would like to create an angular directive, which wrap jquery autocomplete:

'use strict'
angular.module('nsi')
  .directive('autoComplete', function () {
    return {
      restrict: 'A',
      scope: {
        httpService: "=",
        renderItem: '=',
        ngModel: '=',
        minLength: '=',
        onSelect: '='
      },
      link: function (scope, elem) {
        elem.autocomplete({
          source: function (request, response) {
            scope.httpService(request.term).then(function (data) {
              response(
                $.map(data.items, function (item) {
                    return scope.renderItem(item);
                  }
                )
              );
            });
          },
          minLength: scope.minLength,
          select: function (event, ui) {
            if (scope.onSelect) {
              scope.onSelect(ui.item.item);
            }
            scope.$apply(function () {
              scope.ngModel = ui.item.item;
            });
          }
        })
        ;
      }
    };
  });

in my controller I initialize necessary parameters for it:

$scope.supplierRender = function(item){
    return {
      label: item.supplierName,
      value: item.supplierName,
      item: item
    }
  };
  $scope.httpSupplierService = function(suggest){
    return SupplierService.getSuppliers('%' + suggest + '%');
  };

  $scope.supplierSelect = function(val) {
    $scope.employee.supplier.id = val.id;
  };

In my html view I start my directive:

 <input              type="text"
                     auto-complete
                     min-length="3"
                     http-service="httpSupplierService"
                     render-item="supplierRender"
                     on-select="supplierSelect"
                     ng-model="employee.supplier"
                     class="form-control"
                     value="employee.supplier.supplierName" />

The problem is, that when I am opening my employee in edit window, in autocomplete input i see [object Object]. So the queston is: "How to bind ngModel to employee.Supplier", and value of input to specific field of object (employee.supplier.supplierName)

Problem courtesy of: Yuriy Mayorov

Solution

Try to set ng-model:

ng-model="employee.supplier.supplierName"

Generally <input> takes value from directive ng-model so you don't need provide value=..

This is a test: Fiddle

The value=.. attribute works when ng-model not defined for input

Solution courtesy of: Maxim Shoustin

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.