AngularJS ng-if ng-repeat下使用ng-model
2015-07-28 14:58
801 查看
因为ng-if, ng-repeat 会创建新的scope, 在ng-if 内使用ng-model, 需要加上$parent, 例如:$parent,.newVal, 才能在全局的scope中用$scope.newVal访问。
实例如下:
HTML 文件:
JS 文件:
在text框中输入值test, 点击button, 输出为 : undefined
该ng-model 为$parent.newVal:
在text框中输入值test, 点击button, 输出为:test
所以在ng-if 内使用ng-model, 需要加上$parent, 例如:$parent,.newVal, 才能在全局的scope中用$scope.newVal访问。
实例如下:
HTML 文件:
<pre name="code" class="html">
<pre name="code" class="html"> <div class="col-lg-8" ng-init="isShow = true"> <input type="text" class="form-control input-checkbox" id="inputTest" ng-model="newVal" ng-if="isShow" > <input type="button" value="test" ng-click="getVal()" /> </div><span style="font-family: Arial, Helvetica, sans-serif;"> </span>
JS 文件:
$scope.getVal = function(){ console.log($scope.newVal); }
在text框中输入值test, 点击button, 输出为 : undefined
该ng-model 为$parent.newVal:
<div class="col-lg-8" ng-init="isShow = true"> <input type="text" class="form-control input-checkbox" id="inputTest" ng-model="$parent.newVal" ng-if="isShow" > <input type="button" value="test" ng-click="getVal()" /> </div>
在text框中输入值test, 点击button, 输出为:test
所以在ng-if 内使用ng-model, 需要加上$parent, 例如:$parent,.newVal, 才能在全局的scope中用$scope.newVal访问。
相关文章推荐
- angularJS最近总结一些零碎的知识点
- (转)Angular中的拦截器Interceptor
- angularJS学习笔记之一
- AngularJS 中的 Promise 和 设计模式
- AngularJS Directive 隔离 Scope 数据交互
- AngularJS学习笔记(五)
- 简话Angular 08 Angular ajax
- 每天一个小知识点15(Angularjs总结三)
- AngularJS学习笔记(四)
- 使用Spring Boot和Gradle创建AngularJS项目
- AngularJS 之 Factory vs Service vs Provider
- angularjs controller的两种写法
- AngularJS的一些基本样式初窥
- Angular简要概述一些概念
- AngularJS学习笔记
- angularJS directive自定义标签和属性
- AngulaJS实战总结, 带你进入AngularJS世界.
- 20个免费的 AngularJS 资源和开发教程
- zoj2773 Triangular Sums
- AngularJS的一些基本样式初窥