2013-04-20 6 views
6

मैं इस सुविधा का उपयोग परिवर्तन के लिए वस्तुओं की एक सरणी को देखने के लिए:कोणीय में परिवर्तित ऑब्जेक्ट को कैसे प्राप्त करें?

$scope.$watch('Data', function (newVal) { /*...*/ }, true); 

मैं कैसे एक वस्तु ताकि मैं इसे एक सरणी में धक्का कर सकते हैं, जिसमें संपत्ति बदल दिया गया है मिल सकता है? उदाहरण के लिए:

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

myApp.factory("Data", function(){ 
var Data = [{id:1, property: "Random"}, {id:2, property: "Random again"}]; 
return Data; 
}); 

var myBigArray = []; 

function tableCtrl($scope, Data){ 
    $scope.TheData = Data; 
    $scope.$watch("TheData", function() { 

    //Here an object should be pushed 
    myBigArray.push(">>Object in which property has been changed <<<"); 
    }, true); 
} 
+1

देखना चाहते हैं कि आइटम बदल रहे हैं मदद मिलेगी। उस बिंदु पर वस्तु तक पहुंच हो सकती है। दिखाया गया कोड बहुत सरल है। एक डेमो बनाएं जो उपयोग केस दिखाता है। इसके अलावा आपको कोणीय के बाहर ग्लोबल के रूप में संग्रहीत सरणी की आवश्यकता क्यों है? – charlietfl

उत्तर

2

मैं कोणीय में वर्तमान में एक तरह से नहीं दिख रहा है बदली हुई वस्तु प्राप्त करने के लिए ... मैं तुम्हें नई सरणी पार और पुराने सरणी के साथ मतभेदों को खोजने के लिए प्रयास करने की आवश्यकता हो सकती है पर शक। ..

+0

अब 2015 है। क्या इस विषय पर कोई अपडेट है? मैंने इस पूरे दिन बिताया कि किस वस्तु को किसी भाग्य के साथ बदला जा रहा है उसका पता लगाने के लिए। गहरी घड़ी और मैन्युअल तुलना का उपयोग करने से नफरत है। – Codism

0

अभी भी $ घड़ी के लिए इस तरह कोई विकल्प नहीं है, लेकिन आप उस के लिए jQuery प्लगइन का उपयोग कर सकते हैं, http://archive.plugins.jquery.com/project/jquery-diff

मैं कार्यान्वित पूर्ववत/$ घड़ी का उपयोग AngularJS के साथ फिर से करना, mB इस मदद कर सकते हैं

//History Manager Factory 
.factory('HistoryManager', function() { 
    return function(scope) { 

     this.container = Array(); 
     this.index = -1; 
     this.lock = false; 

     //Insert new step into array of steps 
     this.pushDo = function() { 
      //we make sure that we have real changes by converting to json, 
      //and getting rid of all hash changes 
      if(this.container.length == 0 || (angular.toJson(scope.widgetSlider) != angular.toJson(this.container[this.index][0]))) { 
       //check if current change didn't came from "undo" change' 
       if(this.lock) { 
        return; 
       } 
       //Cutting array, from current index, because of new change added 
       if(this.index < this.container.length-1) { 
        this.container = this.container.slice(0, this.index+1); 
       } 

       var currentStepSlider = angular.copy(scope.widgetSlider); 
       var selectedWidgetIndex = scope.widgetSlider.widgets.indexOf(scope.widgetCurrent); 
       //Initialising index, because of new "Do" added 
       this.index = this.container.length; 
       this.container.push([currentStepSlider, selectedWidgetIndex]); 

       if (this.onDo) { 
        this.onDo(); 
       } 
      } 
     } 


     //Upon undo returns previous do 
     this.undo = function() { 
      this.lock = true; 
      if(this.index>0){ 
       this.index--; 
       scope.widgetSlider = angular.copy(this.container[this.index][0]); 
       var selectedWidgetIndex = this.container[this.index][1]; 
       scope.widgetCurrent = scope.widgetSlider.widgets[selectedWidgetIndex]; 
      } 
      this.lock = false; 
     } 

     //Upon redo returns next do 
     this.redo = function() { 
      if(this.index < this.container.length-1) { 
       this.index++; 
       scope.widgetSlider = angular.copy(this.container[this.index][0]); 
       var selectedWidgetIndex = this.container[this.index][1]; 
       scope.widgetCurrent = scope.widgetSlider.widgets[selectedWidgetIndex]; 
      } 
     } 
    } 
}) 

;

2

संपादित करें: ध्यान दें कि यह समाधान खराब अभ्यास साबित हुआ है क्योंकि यह बहुत सारे दर्शक को जोड़ रहा है, जो कुछ ऐसा नहीं है जिसे आप चाहते हैं क्योंकि इसका प्रदर्शन जुर्माना है।

=======

मैं अंत में इस समाधान के साथ आया था:

items.query(function (result) { 
    _(result).each(function (item, i) { 
     $scope.items.push(item); 
     $scope.$watch('items[' + i + ']' , function(){ 
      console.log(item); // This is the item that changed. 
     }, true); 
    }); 
}); 
+0

मुझे पता है कि यह उत्तर 2 साल से अधिक पुराना है, लेकिन यह एक Google खोज पर आया है, इसलिए मैं इसमें एक चेतावनी जोड़ने जा रहा हूं। आपको वास्तव में सावधान रहना होगा कि आप कितनी घड़ियों को जोड़ते हैं। यदि सूची बहुत बड़ी हो जाती है तो सूची में प्रत्येक आइटम के लिए एक घड़ी जोड़ना वास्तव में आपके ऐप को धीमा कर सकता है। –

+1

@D_Naish मैं पूरी तरह से सहमत हूं। उसके बाद जब मैंने जवाब पोस्ट किया था (मुझे लगता है कि कई लोगों के साथ) यह उन घड़ियों के बारे में पता नहीं था जो आप घड़ियों के साथ कर सकते हैं। यह देखने के लिए मजेदार है कि चीजें कितनी तेजी से बदलती हैं। टिप्पणी के लिए धन्यवाद! –

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

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