ionic ng-model controller中取不到值(input)
来源:互联网 发布:彩虹六号配件知乎 编辑:程序博客网 时间:2024/05/17 08:47
一、问题描述在使用ionic的时候,对于input绑定ng-model默认情况下载控制器中获取不到改变的值
1.在自定义控制器中获取不到改变的值
2.在页面绑定可以
原因:
1.ng-model取不到值是因为$scope作用域的问题
2.<ion-content>等指令也有自己的控制器,input的ng-model很有可能绑定到了指令的$scope上了
解决方案:
对于input的ng-model绑定使用对象object方式绑定,可以跨作用于访问。
controller中定义一个集合:
$scope.data ={ text1 : "", text2: "",}
页面中分别定义 ng-model="data.text1",ng-model="data.text2"。
在controller中直接取$scope.data.text1就能得到<input>或者<textarea>中的值了
特别说明:初始化绑定需要是object对象。
示例如下:
HTMl:
<body ng-app="page3" ng-controller="MyCtrl"> <ion-header-bar class="bar-positive" align-title="left"> <buttons> <button class="button icon-button" ng-click="doSomething()"> <i class="icon ion-chevron-left"></i> </button> </buttons> <h1 class="title">链接测试页面1</h1> </ion-header-bar> <ion-content> <div class="list"> <div class="item"> 当前地址:{{location.href}} </div> <div class="item"> <a href="http://www.gongjuji.net">www.gongjuji.net</a> </div> <div class="item"> <a href="http://www.gongjuji.net/Content/Files/HostsOperate(V1.0.0.2).zip">http://www.gongjuji.net/Content/Files/HostsOperate(V1.0.0.2).zip</a> </div> <div class="item item-input"> <input type="text" ng-model="data.txt1" ng-change="txtChange();" /> </div> <div class="item item-assertive" ng-click="showTxt();"> 显示文本 </div> </div> </ion-content>js:
angular.module('page3', ['ionic']).controller('MyCtrl', function ($scope) { $scope.doSomething = function () { try { //在inappbrowser中window.close()方法被禁用,也不会抛出异常 window.close(); } catch (e) { alert(e.message); } } $scope.txtChange = function () { console.info($scope.data.txt1); } $scope.showTxt = function () { $scope.data.txt1 = "张无忌"; } //初始化设置,data必须为object类型 $scope.data = { txt1: "张三丰" };});
更多:cordova-plugin-file 文件操作整理(三)
Apache Cordova开发环境搭建(二)VS Code
Cordova Ajax请求跨域问题整理
0 0
- ionic ng-model controller中取不到值(input)
- ionic ng-model controller中取不到值(input)
- ng-repeat产生的ng-model中取不到值的问题
- ionic中获取ng-model绑定的值undefined?
- angular 控制器controller里获取不到ng-model的值,获取为undefine
- angularJs 取<input type='file'>中文件名 (ng-model获取不了)
- 郁闷!ionic中获取ng-model绑定的值为undefined如何解决
- 郁闷!ionic中获取ng-model绑定的值为undefined如何解决
- AngularJS ng-model获取不到WdatePicker值的解决方案
- AngularJs ng-model在input中无效,js无法调用
- js取不到input的值
- <input>标签用val()取不到值
- 关于input取不到值的可能性
- controller中取不到@Value配置的值——spring父子容器
- SpringMVC 的Model值传到JSP页面中,用EL表达试取不到值
- AngualrJS(四)input元素的ng-model属性
- 如何在ng-if中取得ng-model的值
- Ng-model does not update controller value
- 前端代码调试:Webstorm调试js
- Oracle存储过程实例
- UML建模之活动图介绍(Activity Diagram)
- 算法的力量万变不离其宗
- 银联接入--android热点问题解析
- ionic ng-model controller中取不到值(input)
- JAVAWEB开发之文件的上传与下载(开源组件commons-fileupload的详细使用)
- java的(PO,VO,TO,BO,DAO,POJO)解释
- 创建很多子窗口1,再在子窗口1创建子窗口2
- UML建模之状态图(Statechart Diagram)
- UML建模之部署图(Deployment Diagram)
- MySQL锁表解决方法
- Drupal—关于Drupal8的详细安装建站教程
- 48:Longest Common Prefix