2013-08-20 8 views
173

के अंदर काम नहीं करता है, यहां समस्या दिखाई देने वाली पहेली है। http://jsfiddle.net/Erk4V/1/Angularjs ng-model ng-if

ऐसा प्रतीत होता है कि अगर मेरे पास एनजी-मॉडल के अंदर एक एनजी-मॉडल है, तो मॉडल अपेक्षा के अनुसार काम नहीं करता है।

मुझे आश्चर्य है कि यह एक बग है या यदि मैं उचित उपयोग को गलत समझ रहा हूं।

<div ng-app > 
    <div ng-controller="main"> 

     Test A: {{testa}}<br /> 
     Test B: {{testb}}<br /> 
     Test C: {{testc}}<br /> 

     <div> 
      testa (without ng-if): <input type="checkbox" ng-model="testa" /> 
     </div> 
     <div ng-if="!testa"> 
      testb (with ng-if): <input type="checkbox" ng-model="testb" /> 
     </div> 
     <div ng-if="!someothervar"> 
      testc (with ng-if): <input type="checkbox" ng-model="testc" /> 
     </div> 

    </div> 
</div> 
+2

एक समाधान आप के बजाय एनजी-अगर एनजी शो = "हालत" का उपयोग कर सकते हैं। यह काम करना चाहिए। –

+0

अच्छा सवाल, धन्यवाद –

+0

मुझे लगता है कि अब यह कोई मुद्दा नहीं है कि कोई 'नियंत्रक' का उपयोग कर सकता है? – jamiebarrow

उत्तर

191

ng-if निर्देश, अन्य निर्देशों की तरह एक बच्चे गुंजाइश पैदा करता है। नीचे स्क्रिप्ट (या this jsfiddle)

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular.min.js"></script> 
 

 
<script> 
 
    function main($scope) { 
 
     $scope.testa = false; 
 
     $scope.testb = false; 
 
     $scope.testc = false; 
 
     $scope.obj = {test: false}; 
 
    } 
 
</script> 
 

 
<div ng-app > 
 
    <div ng-controller="main"> 
 
     
 
     Test A: {{testa}}<br /> 
 
     Test B: {{testb}}<br /> 
 
     Test C: {{testc}}<br /> 
 
     {{obj.test}} 
 
     
 
     <div> 
 
      testa (without ng-if): <input type="checkbox" ng-model="testa" /> 
 
     </div> 
 
     <div ng-if="!testa"> 
 
      testb (with ng-if): <input type="checkbox" ng-model="testb" /> {{testb}} 
 
     </div> 
 
     <div ng-if="!someothervar"> 
 
      testc (with ng-if): <input type="checkbox" ng-model="testc" /> 
 
     </div> 
 
     <div ng-if="!someothervar"> 
 
      object (with ng-if): <input type="checkbox" ng-model="obj.test" /> 
 
     </div> 
 
     
 
    </div> 
 
</div>

तो, अपने चेकबॉक्स बदलता है testb बच्चे दायरे के अंदर नहीं, बल्कि बाहरी माता पिता गुंजाइश देखें।

नोट, कि यदि आप मूल दायरे में डेटा को संशोधित करना चाहते हैं, तो आपको किसी वस्तु के आंतरिक गुणों को संशोधित करने की आवश्यकता होगी जैसे कि मैंने जो अंतिम div जोड़ा है।

+1

मैं मुख्य नियंत्रक फ़ंक्शन के भीतर से ng-if के दायरे को कैसे एक्सेस करूं? थोड़ा निराशाजनक। इसके लिए क्या कारण है? –

+0

कभी नहीं, @sza ने अभी जवाब दिया है^प्रश्न। मैं इस जवाब को सही के रूप में चिह्नित करने जा रहा हूं, क्योंकि यह सही कारण बताता है कि मुझे समस्याएं थीं। –

+16

यह एक कारण है कि उदाहरण के मुताबिक, स्कोप गुणों को संशोधित करने के बजाय, सीधे अपने दायरे पर एक युक्त ऑब्जेक्ट का उपयोग करना आम है: '$ scope.obj = {...}' और 'ng-model =" obj.someProperty "' इस सीमा पर विजय प्राप्त करता है। – wulftone

185

आप $parent का उपयोग मॉडल इस

<input type="checkbox" ng-model="$parent.testb" /> 
+14

तो मेरे पास 'ng-model =" $ parent। $ Parent.foo' है क्योंकि मैं पहले से ही 'ng-repeat' के साथ एक दायरे में हूं - क्या यह वास्तव में सबसे अच्छा तरीका है? – chovy

+4

यह वास्तव में भ्रमित है। वह? और एनजी-छिपाने का अपना दायरा क्यों नहीं है? –

+5

पुन @ गॉल: संभवतः एनजी-छुपा/एनजी-शो वर्तमान डोम पर चल रहा है और केवल एक सीएसएस वर्ग को जोड़/हटाएं, जबकि ng-if/ng- डीओएम के साथ सभी मक को स्विच/एनजी-दोहराएं और अतिरिक्त स्थिति का ट्रैक रखें। समझदार लगता है। – trisweb

44

आप उपयोग कर सकते हैं ngHide(या ngShow) निर्देश की तरह माता-पिता दायरे में परिभाषित करने का उल्लेख कर सकते हैं। यह ngIf करता है के रूप में बच्चे के दायरे नहीं बनाता है।

<div ng-hide="testa"> 
+3

क्यों 'ngIf' फिर बच्चे का दायरा बनाते हैं? मेरे लिए बहुत अजीब लगता है। – freeall

+4

Zsong उत्तर की टिप्पणियों पर ध्यान दें। 'Ng-hide' html को नहीं बदलता है संरचना। यह बस सीएसएस शैलियों को बदलता है। 'ng-if' अधिक जटिल है: यह स्थिति के आधार पर HTML भागों को हटा देता है और सम्मिलित करता है। यह राज्य को स्टोर करने के लिए बाल गुंजाइश बनाता है (कम से कम इसे छुपा एचटीएमएल भाग स्टोर करना चाहिए)। –

2

हां, एनजी-छुपाएं (या एनजी-शो) निर्देश बाल दायरा नहीं बनाएगा।

यहाँ मेरी बात है:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular.min.js"></script> 

<script> 
    function main($scope) { 
     $scope.testa = false; 
     $scope.testb = false; 
     $scope.testc = false; 
     $scope.testd = false; 
    } 
</script> 

<div ng-app > 
    <div ng-controller="main"> 

     Test A: {{testa}}<br /> 
     Test B: {{testb}}<br /> 
     Test C: {{testc}}<br /> 
     Test D: {{testd}}<br /> 

     <div> 
      testa (without ng-if): <input type="checkbox" ng-model="testa" /> 
     </div> 
     <div ng-if="!testa"> 
      testb (with ng-if): <input type="checkbox" ng-model="$parent.testb" /> 
     </div> 
     <div ng-show="!testa"> 
      testc (with ng-show): <input type="checkbox" ng-model="testc" /> 
     </div> 
     <div ng-hide="testa"> 
      testd (with ng-hide): <input type="checkbox" ng-model="testd" /> 
     </div> 

    </div> 
</div> 

http://jsfiddle.net/bn64Lrzu/

-1

आप इस तरह यह कर सकते हैं और आप आधुनिक समारोह सही काम करेंगे मुझे पता है अगर आप एक कोड कलम चाहते हैं

<div ng-repeat="icon in icons">     
       <div class="row" ng-if="$index % 3 == 0 "> 
        <i class="col col-33 {{icons[$index + n].icon}} custom-icon"></i> 
        <i class="col col-33 {{icons[$index + n + 1].icon}} custom-icon"></i> 
        <i class="col col-33 {{icons[$index + n + 2].icon}} custom-icon"></i> 
       </div> 
     </div> 
3

हमारे पास यह कई अन्य मामलों में था, हमने आंतरिक रूप से निर्णय लिया कि हमेशा नियंत्रक/निर्देश के लिए एक रैपर होना चाहिए ताकि हमें इसके बारे में सोचने की आवश्यकता न हो। यहां हमारे रैपर के साथ उदाहरण है।

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular.min.js"></script> 

<script> 
    function main($scope) { 
     $scope.thisScope = $scope; 
     $scope.testa = false; 
     $scope.testb = false; 
     $scope.testc = false; 
     $scope.testd = false; 
    } 
</script> 

<div ng-app > 
    <div ng-controller="main"> 

     Test A: {{testa}}<br /> 
     Test B: {{testb}}<br /> 
     Test C: {{testc}}<br /> 
     Test D: {{testd}}<br /> 

     <div> 
      testa (without ng-if): <input type="checkbox" ng-model="thisScope.testa" /> 
     </div> 
     <div ng-if="!testa"> 
      testb (with ng-if): <input type="checkbox" ng-model="thisScope.testb" /> 
     </div> 
     <div ng-show="!testa"> 
      testc (with ng-show): <input type="checkbox" ng-model="thisScope.testc" /> 
     </div> 
     <div ng-hide="testa"> 
      testd (with ng-hide): <input type="checkbox" ng-model="thisScope.testd" /> 
     </div> 

    </div> 
</div> 

होप्स इस मदद करता है, Yishay

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