2012-08-01 11 views
15

क्या कोई जानता है कि AngularJS का उपयोग करके डेटा विशेषता में एक इंटरपोलेटेड मान को कैसे बांधना है?AngularJS डेटा विशेषता में मूल्य मान

<input type="text" data-custom-id="{{ record.id }}" /> 

कोणीय तत्व की संरचना के अलावा कोणीय उस मान को अलग नहीं कर रहा है। इसे ठीक करने का कोई तरीका?

+0

क्या आप jsfiddle उदाहरण प्रदान कर सकते हैं? –

उत्तर

6

ऐसा लगता है कि कोई समस्या नहीं है। टेम्पलेट को पार्स किया गया है और मेरा नियंत्रक डेटा डाउनलोड कर रहा था, लेकिन जब टेम्पलेट को पार्स किया जा रहा था तो डेटा अभी तक नहीं था। और जिस निर्देश को मैंने रखा है, उस समय डेटा को उस समय की आवश्यकता है जब वह खाली मैक्रो डेटा उठा रहा हो।

तरह से है कि मैं हल इस $ घड़ी कमांड के साथ किया गया था:

$scope.$watch('ready', function() { 
    if($scope.ready == true) { 
    //now the data-id attribute works 
    } 
}); 

फिर जब नियंत्रक सभी ajax सामान लोड हो जाए तो आप इस करते हैं:

$scope.ready = true; 
+0

आपके द्वारा प्रश्न में बात किए गए रिकॉर्ड.आईडी के लिए प्रासंगिक विकल्प कैसे हैं? – Hengjie

+0

मैंने कोड को और अधिक प्रासंगिक होने के लिए अपडेट किया है। – matsko

1

यह करने के लिए की तरह लग रहा

// for the purpose of this example let's assume that variables '$q' and 'scope' are 
// available in the current lexical scope (they could have been injected or passed in). 

function asyncGreet(name) { 
    var deferred = $q.defer(); 

    setTimeout(function() { 
    // since this fn executes async in a future turn of the event loop, we need to wrap 
    // our code into an $apply call so that the model changes are properly observed. 
    scope.$apply(function() { 
     if (okToGreet(name)) { 
     deferred.resolve('Hello, ' + name + '!'); 
     } else { 
     deferred.reject('Greeting ' + name + ' is not allowed.'); 
     } 
    }); 
    }, 1000); 

    return deferred.promise; 
} 

var promise = asyncGreet('Robin Hood'); 
promise.then(function(greeting) { 
    alert('Success: ' + greeting); 
}, function(reason) { 
    alert('Failed: ' + reason); 
); 

संपादित करें:: मुझे क्या तुम सच में करने के बाद कर रहे हैं एक Promise/Deferred है ठीक है, यहाँ एक सरल परीक्षा है एक नियंत्रक के साथ एक वादा का उपयोग कर और बाध्यकारी की मिसाल:

var app = angular.module('myApp', []); 

app.controller('MyCtrl', function($scope, $q) { 
    var deferredGreeting = $q.defer(); 
    $scope.greeting = deferredGreeting.promise; 

    /** 
    * immediately resolves the greeting promise 
    */ 
    $scope.greet = function() { 
     deferredGreeting.resolve('Hello, welcome to the future!'); 
    }; 

    /** 
    * resolves the greeting promise with a new promise that will be fulfilled in 1 second 
    */ 
    $scope.greetInTheFuture = function() { 
     var d = $q.defer(); 
     deferredGreeting.resolve(d.promise); 

     setTimeout(function() { 
      $scope.$apply(function() { 
       d.resolve('Hi! (delayed)'); 
      }); 
     }, 1000); 
    }; 
});​ 

कार्य JSFiddle: http://jsfiddle.net/dain/QjnML/4/

मूल रूप से विचार है कि आप वादा बाध्य कर सकते हैं और एक बार async प्रतिक्रिया यह निराकरण यह पूरा किया जाएगा है।

+0

यह बहुत अच्छा है। लेकिन, क्या आप कृपया एक उदाहरण प्रदान कर सकते हैं जो मेरे प्रश्न से बेहतर है? मुझे समझने में मुश्किल हो रही है। धन्यवाद :) – matsko

+0

इसका मूल रूप से सीधे दस्तावेज़ बनाते हैं ... उसे कुछ सवाल देना अच्छा होगा जो उसके प्रश्न से संबंधित है। – Nix

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