我使用的工具:我正在使用离子,我使用chrome和ubutu os。
我正在尝试为数据绑定做一个演示项目请检查我的plunknr https://plnkr.co/edit/CFSgfWDH5UY0XaDjRoBK?p=preview
这里我试图将用户输入的数据绑定到formPost对象,但是在console.log中我得到像({Username: undefined, Name: undefined, EmailID: undefined, Password: undefined}
)一样的未定义,如果有人发现任何错误请告诉我
答案 0 :(得分:1)
这是 HTMl
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="app.js"></script>
</head>
<body ng-app="starter">
<ion-pane ng-controller="formValidation">
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content>
<div class="list">
<label class="item item-input item-stacked-label">
<span class="input-label">User Name</span>
<input type="text" placeholder="John" ng-model="username">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Name</span>
<input type="text" placeholder="Suhr" ng-model="name">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Email</span>
<input type="text" placeholder="john@suhr.com" ng-model="emailid">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Password</span>
<input type="text" placeholder="john@suhr.com" ng-model="password">
</label>
</div>
<button class="button button-block button-positive" ng-click="submit(username,name,emailid,password)">
sign up
</button>
</ion-content>
</ion-pane>
</body>
</html>
<强> JS 强>
// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.controller('formValidation',function($scope,$http){
$scope.submit=function(username,name,emilid,password){
var formPost = {
"Username":username,
"Name":name,
"EmailID":emilid,
"Password":password
};
console.log(formPost);
$http.post("http://aflaree.com/qrcodeservice/Service1.svc/Signupsupervisor",formPost)
.success(function(response){
console.log(response);
})
.error(function(response){
console.log(response);
});
};
});
工作Pluknr
修改强>
在纯 Angular 中, 您在问题中显示的方式将会有效,您可以看到此JsFiddle以供参考。
但是在进入离子时,这可能无法按预期工作,因为 由于在Ionic源代码中定义了该指令的方式,因此该问题与ion-content有关。它专门创建了自己的子女范围。
.directive('ionContent', [
'$parse',
'$timeout',
'$ionicScrollDelegate',
'$controller',
'$ionicBind',
function($parse, $timeout, $ionicScrollDelegate, $controller, $ionicBind) {
return {
restrict: 'E',
replace: true,
transclude: true,
require: '^?ionNavView',
scope: true,
template:
'<div class="scroll-content">' +
'<div class="scroll"></div>' +
'</div>',
由于这一点以及继承在JavaScript中的工作方式,您不能使用双向绑定。基本上你的$ scope.submit是在你的控制器中定义的,并且只是作为离子内容范围的新原语创建的。它不像对象那样被引用复制。
阅读this article。理解这一切如何运作并克服这些问题至关重要。
答案 1 :(得分:0)
首先,你的&#34; EmailID&#34;:$ scope.emilid,它应该是$ scope.emailid
问题出在ng-controller上,应将其定义为<ion-content>
的子项,您可以查看以下代码:
HTML:
<body ng-app="starter">
<ion-pane >
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content>
<div ng-controller="formValidation">
<div class="list">
<label class="item item-input item-stacked-label">
<span class="input-label">User Name</span>
<input type="text" placeholder="John" ng-model="username">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Name</span>
<input type="text" placeholder="Suhr" ng-model="name">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Email</span>
<input type="text" placeholder="john@suhr.com" ng-model="emailid">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Password</span>
<input type="text" placeholder="john@suhr.com" ng-model="password">
</label>
</div>
<button class="button button-block button-positive" ng-click="submit()">
sign up
</button>
</div>
</ion-content>
</ion-pane>
角:
var app = angular.module('starter', []);
app.controller('formValidation', function($scope) {
$scope.submit=function(){
var formPost = {
"Username":$scope.username,
"Name":$scope.name,
"EmailID":$scope.emailid,
"Password":$scope.password
};
console.log(formPost);
}
});
答案 2 :(得分:0)
将数据绑定到HTML模板的最简单方法如下:
模板:
<input [(ngModel)]="name">
.ts code:
name:any= "Tanzeel"
因此[()]
用于双向绑定。