Android ng单击不调用函数--对于angular和ionic非常新
试图将数据插入sqlite db,但数据是静态的,我正在通过函数传递数据,但ng click不起作用。因为我是新手,所以请详细回答…提前谢谢Android ng单击不调用函数--对于angular和ionic非常新,android,angularjs,ionic-framework,ngcordova,Android,Angularjs,Ionic Framework,Ngcordova,试图将数据插入sqlite db,但数据是静态的,我正在通过函数传递数据,但ng click不起作用。因为我是新手,所以请详细回答…提前谢谢 //this is my module var db=null; var myApp=angular.module('starter', ['ionic','ngCordova']) .run(function($ionicPlatform,$cordovaSQLite) { $ion
//this is my module
var db=null;
var myApp=angular.module('starter', ['ionic','ngCordova'])
.run(function($ionicPlatform,$cordovaSQLite) {
$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();
}
db = $cordovaSQLite.openDB({name:"my.db",location:'default'});
$cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS user(id integer primary key, username text, password text)")
});
})
//this is my controller
myApp.controller('SignInCtrl',['$cordovaSQLite', function($scope, $cordovaSQLite) {
$scope.insert=function(username,password){
alert("hii");
var query="INSERT INTO user(username,password) VALUES(?,?)";
$cordovaSQLite.execute(db.query,[username,password]).then(function(res){
console.log("Insert ID ->" + res.insertId);
},
function(err){
console.error(err);
});
}
$scope.select = function(username) {
var query = "SELECT username, password FROM user WHERE username = ?";
$cordovaSQLite.execute(db, query, [password]).then(function(res) {
if(res.rows.length > 0) {
console.log("SELECTED -> " + res.rows.item(0).username + " " + res.rows.item(0).password);
} else {
console.log("No results found");
}
}, function (err) {
console.error(err);
});
}
}])
//this is my page
<ion view view-title="Sign-In" name="Login-View">
<ion content class="padding">
<div class="list list-inset">
<label class="item item-input">
<input type="text" placeholder="Username" ng-model="data.username">
</label>
<label class="item item-input">
<input type="password" placeholder="Password" ng-model="data.password"></input>
</label>
</div>
</ion>
<button class="button button-block button-positive" ng-click="login(data)">Login</button>
</ion>
<br>
<a href="#/Signup" class="button button-block button-stable">Sign Up</a>
<a href="#/ForgotPassword" class="button button-block button-stable">Forgot Password</a>
<a href="#/orderpage" class="button button-block button-stable">Order page</a>
<button class="button button-block button-positive" ng-click="insert('ran','badu')">insert</button>
<button class="button button-block button-positive" ng-click="select('ran')">select</button>
//this is my index page
<body ng-app="starter">
<ion-header-bar class="bar-positive">
<h1 class="title">Billing system</h1>
<button class="button icon ion-navicon"></button>
<button class="button icon ion-search"></button>
</ion-header-bar>
<!-- <ion-content > -->
<!-- <P>I am in index</P>
<a href="#/Login">Loginpage</a> -->
<!-- </ion-content> -->
<ion-nav-view class="slide-left-right"></ion-nav-view>
</body>
//this is my route
.config(function($stateProvider,$urlRouterProvider) {
$stateProvider
.state('Login',{
url:'/Login',
templateUrl:'templates/Login.html',
controller:'SignInCtrl'
})
.state('Signup',{
url:'/Signup',
templateUrl:'templates/Signup.html',
controller:'SignInCtrl'
})
.state('ForgotPassword',{
url:'/ForgotPassword',
templateUrl:'templates/ForgotPassword.html',
controller:'SignInCtrl'
})
.state('orderpage',{
url:'/orderpage',
templateUrl:'templates/orderpage.html',
controller:'SignInCtrl'
})
$urlRouterProvider.otherwise('/Login');
})
//这是我的模块
var-db=null;
var myApp=angular.module('starter',['ionic','ngCordova']))
.run(函数($ionicPlatform,$cordovaSQLite){
$ionicPlatform.ready(函数(){
if(window.cordova&&window.cordova.plugins.Keyboard){
//默认情况下隐藏附件栏(删除此选项可在键盘上方显示附件栏)
//表格输入)
插件键盘hideKeyboardAccessoryBar(真);
//除非您知道自己在做什么,否则不要删除此行。它会停止视口
//当文本输入被聚焦时,从捕捉。Ionic在内部为
//更好的键盘体验。
插件。键盘。禁用滚动(真);
}
如果(窗口状态栏){
StatusBar.styleDefault();
}
db=$cordovaSQLite.openDB({name:'my.db',location:'default'});
$cordovaSQLite.execute(db,“如果不存在,则创建表用户(id整数主键、用户名文本、密码文本)”)
});
})
//这是我的控制器
myApp.controller('SignInCtrl',['$cordovaSQLite',函数($scope,$cordovaSQLite){
$scope.insert=函数(用户名、密码){
警报(“hii”);
var query=“插入用户(用户名、密码)值(?,)”;
$cordovaSQLite.execute(db.query,[username,password])。然后(函数(res){
log(“插入ID->”+res.insertId);
},
功能(err){
控制台错误(err);
});
}
$scope.select=函数(用户名){
var query=“从用户名=?”的用户中选择用户名、密码”;
$cordovaSQLite.execute(db,query,[password]),然后(function(res){
如果(res.rows.length>0){
console.log(“选定->”+res.rows.item(0).用户名+“”+res.rows.item(0).密码);
}否则{
console.log(“未找到结果”);
}
},函数(err){
控制台错误(err);
});
}
}])
//这是我的页面
登录
插入
选择
//这是我的索引页
计费系统
//这是我的路线
.config(函数($stateProvider,$urlRouterProvider){
$stateProvider
.state('登录'{
url:“/Login”,
templateUrl:'templates/Login.html',
控制器:'SignInCtrl'
})
.州(“注册”{
url:“/Signup”,
templateUrl:'templates/Signup.html',
控制器:'SignInCtrl'
})
.state('放弃密码'{
url:“/ForgotPassword”,
templateUrl:'templates/ForgotPassword.html',
控制器:'SignInCtrl'
})
.state('orderpage'{
url:“/orderpage”,
templateUrl:'templates/orderpage.html',
控制器:'SignInCtrl'
})
$urlRouterProvider。否则('/Login');
})
查看您的代码登录控制器中不存在login()方法请尝试将登录方法添加到控制器中
myApp.controller('SignInCtrl', ['$cordovaSQLite', function ($scope, $cordovaSQLite) {
$scope.insert = function (username, password) {
alert("hii");
var query = "INSERT INTO user(username,password) VALUES(?,?)";
$cordovaSQLite.execute(db.query, [username, password]).then(function (res) {
console.log("Insert ID ->" + res.insertId);
},
function (err) {
console.error(err);
});
}
$scope.select = function (username) {
var query = "SELECT username, password FROM user WHERE username = ?";
$cordovaSQLite.execute(db, query, [password]).then(function (res) {
if (res.rows.length > 0) {
console.log("SELECTED -> " + res.rows.item(0).username + " " + res.rows.item(0).password);
} else {
console.log("No results found");
}
}, function (err) {
console.error(err);
});
}
$scope.login = function(data){
//Add Login Logic ....:)
console.log(data)
}
}])
您的SignInCtrl声明有错误。更改这行代码
myApp.controller('SignInCtrl',['$cordovaSQLite', function($scope, $cordovaSQLite)
与
这里的问题是使用内联依赖项注入,顺序很重要感谢将$scope添加到函数中,但没有插入数据。获取错误“无法读取未定义的”的属性“事务”将$cordovaSQLite.execute(db.query,[username,password])更改为$cordovaSQLite.execute(db,query,[username,password])。你应该使用“,”而不是“.”。如果这解决了你的问题或者是一个打字错误,那就试试吧。这对我来说是个很大的帮助,因为我是新手。我三天前才开始学习ionic。请提供一些学习概念的好链接。因为你是初学者,我建议你首先阅读角度概念。一本伟大的书(也是官方的)是一本伟大的书()。关于爱奥尼亚,我认为官方的爱奥尼亚框架页面()是一个很好的起点,感谢您的回答。我只是试图通过点击登录页面中的按钮来插入数据。按钮名称为insert和函数insert()。
myApp.controller('SignInCtrl',['$scope','$cordovaSQLite', function($scope, $cordovaSQLite)