2013-08-21 16 views
20

मैं ट्विटर टाइपहेड प्लगइन को लपेटने वाला निर्देश बनाने की कोशिश कर रहा हूं। क्या मैं अब तक है:कोणीय-जेएस निर्देश बनाते हैं जो अद्यतन ng-model

HTML:

<input ng-twitter-typeahead type="text" ng-model="participant" data="exampleData" /> 
{{ participant }} 

मैं 'भागीदार' के मान को अद्यतन करने की है जब मैं Typeahead पर कुछ का चयन करना चाहते हैं। टाइपहेड स्वयं ठीक से काम करता है, लेकिन मैं चयनित मान को कैप्चर नहीं कर सकता। नीचे जावास्क्रिप्ट है:

var app = angular.module('myApp', []) 
app.directive('ngTwitterTypeahead', function() { 
    return { 
    restrict: 'EA', 
    scope: { 
     data: '=' 
    }, 
    link: function ($scope, $element, $attrs) { 
     $element.typeahead($scope.data); 

     $element.bind('typeahead:selected', function(obj, datum) {   
     // I really don't know how to do this part 
     // the variable 'datum' is what I want to be passed to ng-model 
     // I tried things like: 
      // Including the ngModelController and typing: 
      // ngModel.$setViewValue(datum) 
      // but that didn't work. 
    } 
    }; 
}); 

जब मैं अंगुलरजेएस की बात करता हूं तो मुझे कुछ मौलिक याद आ रही है। किसी भी तरह की सहायता का स्वागत किया जाएगा!

संपादित करें **

मैं समाधान मिल गया। मुझे नहीं पता कभी कभी हूँ:

angular.module('siyfion.ngTypeahead', []) 
    .directive('ngTypeahead', function() { 
    return { 
    restrict: 'C', 
    scope: { 
     datasets: '=', 
    ngModel: '=' 
    }, 
    link: function ($scope, $element, $attrs) { 
     $element.typeahead($scope.datasets);  

     $element.bind('typeahead:selected', function(obj, datum) {   
    $scope.$apply(function() { 
    $scope.ngModel = datum; 
    }); 
    })    
    } 
    }; 
}); 

उत्तर

21

आप के निर्देश अंदर ngModel नियंत्रक की आवश्यकता हो सकती। यह आप मॉडल नियंत्रक link समारोह के अंदर करने के लिए एक पहुँच दे देंगे, देखना http://docs.angularjs.org/api/ng.directive:ngModel.NgModelController

यहाँ आप एक उदाहरण है कि कैसे यह वास्तविक जीवन http://suhairhassan.com/2013/05/01/getting-started-with-angularjs-directive.html#.UhSdDOZdXUE

+0

धन्यवाद! मैं पूरी चीज को खत्म कर रहा था, और मैंने इस पर अपने सिर को टक्कर देने से कुछ सीखा है। – user2205763

संबंधित मुद्दे