Angularjs ng-model ไม่ทำงานภายใน ng-if


206

นี่คือซอแสดงปัญหา http://jsfiddle.net/Erk4V/1/

มันจะปรากฏขึ้นหากฉันมี 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>

6
สำหรับวิธีแก้ปัญหาคุณสามารถใช้ ng-show = "CONDITION" แทน ng-if มันควรจะทำงาน
Hari Das

ฉันเข้าใจนี้จะไม่มีปัญหาตอนนี้ที่หนึ่งสามารถใช้controllerAs?
jamiebarrow

ฉันมีปัญหาเดียวกันเมื่อใช้คำสั่งที่มีนัยscope:falseและฉันเพิ่มng-ifองค์ประกอบรอบคำสั่ง - ขอบเขตถูกผูกไว้ในตอนแรก แต่พวกเขาก็แยกออกจากกันหลังจากเฝ้าดูการปรับปรุงหนึ่งในค่าขอบเขต ...
Aprillion

คำตอบ:


223

ng-ifสั่งเช่นคำสั่งอื่น ๆ ที่สร้างขอบเขตเด็ก ดูสคริปต์ด้านล่าง (หรือ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 ได้จากภายในตัวควบคุมหลักหรือไม่ น่าผิดหวังนิดหน่อย เหตุผลนี้คืออะไร
Justin Carlson

ไม่เป็นไร @sza เพิ่งตอบคำถามนั้น ^ ฉันจะทำเครื่องหมายคำตอบนี้ว่าถูกต้องเนื่องจากอธิบายเหตุผลที่แน่นอนว่าฉันมีปัญหา
Justin Carlson

21
นี่คือเหตุผลหนึ่งที่พบเห็นได้ทั่วไปในการใช้วัตถุที่มีอยู่ในขอบเขตของคุณแทนที่จะแก้ไขคุณสมบัติขอบเขตโดยตรงตามที่อธิบายไว้ในตัวอย่าง: $scope.obj = {...}และng-model="obj.someProperty"เอาชนะข้อ จำกัด นี้
wulftone

204

คุณสามารถใช้$parentเพื่ออ้างถึงโมเดลที่กำหนดในขอบเขตพาเรนต์เช่นนี้

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

16
ถ้าอย่างนั้นฉันก็ng-model="$parent.$parent.fooเพราะฉันอยู่ในขอบเขตที่มีng-repeat- นี่เป็นวิธีที่ดีที่สุดจริง ๆ หรือ
chovy

4
นี่มันช่างสับสนจริงๆ ทำไมถึงเป็นอย่างนั้น? และทำไม ng-hide ไม่มีขอบเขตเป็นของตัวเอง
Dominik Goltermann

5
Re @Gaul: น่าจะเป็นเพราะ ng-hide / ng-show ทำงานบน DOM ปัจจุบันและเพียงแค่เพิ่ม / ลบคลาส CSS ในขณะที่ ng-if / ng-switch / ng-repeat โคลนทั้งหมดด้วย DOM และติดตามสถานะพิเศษ . ดูเหมือนสมเหตุสมผล
trisweb

4
Sensible ไม่ใช่คำที่ฉันใช้
Jonathan Dumaine

3
เพิ่มวัตถุในขอบเขตเดิมและเปลี่ยนคุณสมบัติของวัตถุนั้น เช่น ng-model = "myObject.property" สิ่งนี้จะก้าวเท้าเลี่ยงขอบเขต / $ inanity parent ทั้งหมด Google "กฎจุดเชิงมุม" สำหรับข้อมูลเพิ่มเติม
Asmor

50

คุณสามารถใช้คำสั่งngHide (หรือ ngShow) มันไม่ได้สร้างขอบเขตของลูกอย่างที่ทำ

<div ng-hide="testa">

3
เหตุใดจึงngIfสร้างขอบเขตลูก ดูเหมือนจะแปลกมากสำหรับฉัน
freeall

5
ให้ความสนใจกับความคิดเห็นของคำตอบ zsong ng-hideไม่เปลี่ยนโครงสร้าง html มันเพียงแค่เปลี่ยนรูปแบบ CSS ng-ifมีความซับซ้อนมากขึ้น: มันจะลบและแทรกส่วน html ตามเงื่อนไข มันสร้างขอบเขตลูกเพื่อเก็บสถานะ (อย่างน้อยก็ควรเก็บส่วน html ที่ซ่อนอยู่)
Vasiliy Kevroletin

ใช่ทำงานให้ฉัน
Basit

7

เรามีสิ่งนี้ในหลาย ๆ กรณีสิ่งที่เราตัดสินใจภายในคือมีเสื้อคลุมสำหรับตัวควบคุม / คำสั่งเพื่อให้เราไม่จำเป็นต้องคิดเกี่ยวกับมัน นี่คือตัวอย่างของเสื้อคลุมของเรา

<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


3

ใช่คำสั่ง ng-hide (หรือ ng-show) จะไม่สร้างขอบเขตลูก

นี่คือการปฏิบัติของฉัน:

<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/


0

คุณสามารถทำได้เช่นนี้และฟังก์ชั่น mod ของคุณจะทำงานได้อย่างสมบูรณ์แบบแจ้งให้เราทราบหากคุณต้องการปากกาโค้ด

  <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>
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.