6

से दायरे पर एक चर सेट करना मैं $scope पर एक चर, selected.child सेट करने की कोशिश कर रहा हूं ताकि मैं इसे कहीं और उपयोग कर सकूं। मैं अभी भी कोणीय में scopes के लिए नया हूँ, लेकिन यकीन नहीं है कि मैं निर्देश के भीतर से दायरे पर कुछ क्यों सेट नहीं कर सकता। मैं स्कोप फ़ंक्शंस कॉल कर सकता हूं।एंगुलरजेएस - निर्देश

मेरे पास JSfiddle है और कोड नीचे पोस्ट किया गया है।

अग्रिम सहायता के लिए धन्यवाद।

HTML:

<div ng-controller="DashCtrl"> 
    <h3>{{selected.child}}<h3> 

    <div ng-repeat="child in children" select={{child}}> 
     {{child.username}} 
    </div> 
</div> 

जावास्क्रिप्ट:

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

dash.directive('select', function() { 
    return { 
    restrict: "A", 
    scope: false, 
    link: function (scope, element, attrs) { 
     element.bind('click', function() { 
     scope.selected.child = jQuery.parseJSON(attrs.select); //Neither this 
     scope.setSelected(jQuery.parseJSON(attrs.select)); //Nor this is working 

      if (attrs.select != scope.selected) { 
      other_elements = angular.element(document.querySelectorAll('[select]')); 
       for (var i = 0; i < other_elements.length; i++) { 
        elm = jQuery(other_elements[i]); 
        elm.css('background', 'none'); 
       } 
       element.css('background', '#F3E2A9'); 
      } 
     }); 
    } 
    }; 
}); 

dash.controller('DashCtrl', function ($scope) { 
    $scope.setSelected = function (child) { 
    $scope.selected.child = child; 
    }; 

    $scope.children = [{ 
    "age_group": "6-8", 
     "username": "my_child" 
    }, { 
    "age_group": "8-10", 
     "username": "another_child" 
    }]; 

    $scope.selected = { 
    child: "none" 
    }; 


}); 

उत्तर

11

आप $ के लिए एक कॉल याद कर रहे हैं लागू बस नीचे दिए गए के रूप में अपने कोड को संशोधित

  scope.selected.child = jQuery.parseJSON(attrs.select); //Neither this 
      // scope.setSelected(jQuery.parseJSON(attrs.select)); //Nor this is working 
scope.$apply(); 
संबंधित मुद्दे