example else angularjs

else - Angularjs ng-model no funciona dentro de ng-if



ng if else (6)

La directiva ng-if , al igual que otras directivas, crea un ámbito secundario. Ver el script a continuación (o este 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>

Por lo tanto, su casilla de verificación cambia el testb dentro del ámbito hijo, pero no el ámbito padre externo.

Tenga en cuenta que si desea modificar los datos en el ámbito principal, deberá modificar las propiedades internas de un objeto como en el último div que agregué.

Aquí está el violín que muestra el problema. http://jsfiddle.net/Erk4V/1/

Aparece si tengo un modelo ng dentro de un ng-if, el modelo no funciona como se esperaba.

Me pregunto si esto es un error o si no estoy entendiendo el uso correcto.

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


Puede usar $parent para referirse al modelo definido en el ámbito principal como este

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


Puede utilizar la directiva ngHide (o ngShow) . No crea un ámbito hijo como lo hace ngIf.

<div ng-hide="testa">


Puedes hacerlo de esta manera y la función mod funcionará perfectamente. Avísame si quieres un bolígrafo.

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


Sí, la directiva ng-hide (o ng-show) no creará un ámbito secundario.

Aquí está mi práctica:

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


Tuvimos esto en muchos otros casos, lo que decidimos internamente es tener siempre una envoltura para el controlador / directiva para que no tengamos que pensar en ello. Aquí está tu ejemplo con nuestro envoltorio.

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

Espera que esto ayude, Yishay.