2016-03-03 116 views
0



我在jquery-ui自動完成的自動完成angularjs指令傳遞動態數據時遇到了麻煩。這裏是我當前的代碼:動態自動完成指令

HTML:

<div ng-app="peopleApp"> 
    <div ng-controller="indexController"> 
     <label class="input-group-addon input-label">Search:</label> 
     <input class="form-control input-form" id="search" type="text" placeholder="Search here..." auto-complete names="names"> 
     <button ng-click="change()">Change</button> 
    </div> 
</div> 

JS:

所有的數據
var peopleApp = angular.module('peopleApp', []); 


peopleApp.controller('indexController', function($scope, $http, $rootScope, $controller){ 
    $http.post(domainName+url) 
    .then(function(response){ 
     data = response.data.data; 
     $scope.names = data.map(function(obj){ var rObj = []; rObj.push(obj['rank_code']); rObj.push(obj['rank_description']); return rObj; }); 
    }); 

    $scope.change = function(){ 
     $scope.names = ["hnnnnn", "billlll"]; 
    } 
}); 


peopleApp.directive('autoComplete', function(){ 
    return { 
     scope: {names: '='}, 
     link: function(scope, element, attrs){ 
      // alert(JSON.stringify(element)); 
      attrs.$observe('names', function(val){ 
       // scope.info = val; 
       alert(val); 
       scope.names = val; 
      }); 
      element.autocomplete({ 
       source: scope.names, 
       select: function() { 
        // alert('dean'); 
        // iElement.trigger('input'); 
       }, 
       // Sets the min of characters before activating dropdown 
       minLength:2 
      }); 
     } 
    } 
}); 

首先是獲取從API不會自動完成整合。第二我希望當我按下一個按鈕時,將改變的$ scope.names也將被集成到自動完成功能中

+0

不要'jquery'用'angular'混庫。原則上可以這樣做,但有時候更容易找到'angular'的庫。像這樣[ngAutocomplete](http://ngmodules.org/modules/ngAutocomplete) –

+0

那麼你有源動態或不動態的解決方案嗎? –

+0

存在動態源使用的解決方案。例如[角度綁帶頭](http://mgcrea.github.io/angular-strap/#/typeaheads)。 –

回答

1

使用jQuery的工作解決方案autocomplete

jsfiddle上的現場示例。

function DefaultCtrl($scope) { 
 
    $scope.names = ["john", "bill", "charlie", "robert", "alban", "oscar", "marie", "celine", "brad", "drew", "rebecca", "michel", "francis", "jean", "paul", "pierre", "nicolas", "alfred", "gerard", "louis", "albert", "edouard", "benoit", "guillaume", "nicolas", "joseph"]; 
 
    $scope.addName = function() { 
 
    $scope.names.push($scope.name); 
 
    } 
 
} 
 

 
angular.module('MyModule', []) 
 
.controller('DefaultCtrl',DefaultCtrl) 
 
.directive('autoComplete', function($timeout) { 
 
    return { 
 
    restrict: "A", 
 
    scope: { 
 
     uiItems: "=" 
 
    }, 
 
    link: function(scope, iElement, iAttrs) { 
 
     scope.$watchCollection('uiItems', function(val) { 
 
     console.log(val); 
 
     iElement.autocomplete({ 
 
      source: scope.uiItems, 
 
      select: function() { 
 
      $timeout(function() { 
 
       iElement.trigger('input'); 
 
      }, 0); 
 
      } 
 
     }); 
 
     }); 
 

 
    } 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script> 
 
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/flick/jquery-ui.css" rel="stylesheet" type="text/css"> 
 
<div ng-app='MyModule'> 
 
    <div ng-controller='DefaultCtrl'> 
 
    <input auto-complete ui-items="names" ng-model="selected"> selected = {{selected}} 
 
    <br> 
 
    <input placeholder="add name" ng-model="name"> 
 
    <button ng-click="addName()"> 
 
     Add name 
 
    </button> 
 
    <pre>{{names|json}}</pre> 
 
    </div> 
 
</div>

+0

完美答案!謝啦! '範圍。$ watchCollection'真的可以解決這個問題,但是你能解釋一下它的用途嗎?和語法的流程? –

+0

閱讀[文檔](https://docs.angularjs.org/api/ng/type/$rootScope.Scope)。或者看看[這裏](http://www.bennadel.com/blog/2566-scope-watch-vs-watchcollection-in-angularjs.htm)。你將來需要它。 –