如何使用 ng-value post MySQL 中的用户 ID
How can I use the ng-value to post the id of the user in MySQL
所以我正在制作一个移动网络应用程序,我有一个表格,他们可以在其中进行约会并将其保存到 MySQL 服务器中。我需要发送隐藏的用户 ID。我已经在 ng-init 中有了它。我有一个控制器可以获取它,它可以用来把它放在 html 中。我不太擅长 angularjs 在 all.I 主要使用 php 来处理 javascript 中的所有事情和很少的事情。任何帮助将不胜感激。
这是我的表格。
<div class="login-form" ng-controller="userIdCtrl" ng-init="init('<? echo $usernameid; ?>','<? echo $usernameid; ?>')">
<div class="center" style="margin:5px;color:white;">
<h2>Registro</h2>
<form role="form" name="registro" ng-controller="appointmentPostCtrl">
<div class="form-group">
<div class="col-md-9 col-sm-9 col-xs-9">
<select ng-model="especialidad" class="styled-select" required>
<option value="">Especilidad</option>
<?php echo $especialidades ?>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-9 col-sm-9 col-xs-9">
<select ng-model="facilidad" class="styled-select" required>
<option value="">Facilidad</option>
<?php echo $facilidades ?>
</select>
</div>
</div>
<br>
{{ userid }} <!--- test that it works ----->>>
<input ng-model="user" ng-value="{{ userid }}"> <!--- actual part that doesn't works it returned empty----->>>
<input type="date" ng-model="fecha" class="styled-select" style="width:40%; float:left;">
<input type="time" id="timeInput" name="timeInput" ng-model="hora" placeholder="HH:mm:ss" min="08:00:00" max="17:00:00" required />
<br><br>
<div style="margin-top:50px;">
<button ng-click="postData()" class="button button--large " style="background-color:#6CBA45;">Registrar</button>
<ons-button modifier="quiet" class="forgot-password" ><a style="text-decoration:none;color:white;" href="login.php">Cancelar</a></ons-button>
<span id="message">{{ message }}</span>
</div>
</form>
</div>
</div>
控制器一个用于用户 ID,另一个用于处理 post 到 mysql
////////////////// userIdCtrl Controller //////////////////////
app.controller('userIdCtrl', function ($scope) {
$scope.init = function (userid, id) {
$scope.id = id;
$scope.userid = userid;
};
});
//////////// appointment post Controller/////////////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////// ////
app.controller('appointmentPostCtrl', function ($scope, $http) {
$scope.postData = function () {
$scope.message = "";
var error = 0;
/*---------------- this is the part that is no working return as empty------ */
if ($scope.user == "" || $scope.user == null) {
error = 1;
}
/*--------------------------------------------------------------------- */
if ($scope.especialidad == "" || $scope.especialidad == null) {
error = 2;
}
if ($scope.facilidad == "" || $scope.facilidad == null) {
error = 3;
}
if ($scope.fecha == "" || $scope.fecha == null) {
error = 4;
}
if ($scope.hora == "" || $scope.hora == null) {
error = 5;
}
/*---- Data is validated ------ */
if (error == 0) {
var request = $http({
method: "post",
url: "https://www.xxxxxxxx.com/angPost.php",
data: {
user: $scope.user,
especialidad: $scope.especialidad,
facilidad: $scope.facilidad,
fecha: $scope.fecha,
hora: $scope.hora
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
/* Check whether the HTTP Request is Successfull or not. */
request.success(function (data) {
$scope.message = "" + data;
});
} else {
$scope.message = "You have Filled Wrong Details! Error: " + error;
};
};
});
我是不是在 angular 的逻辑上做错了什么,或者我缺少资源?你试过只制作一个控制器,但也不起作用。这是我能得到的最接近错误句柄的信息。我想我需要一条 ng 路线或者一项服务?需要一些光。谢谢。
使用这个 ng-value="userid"
。 ng-value.
不需要花括号
app.controller('userIdCtrl', function ($scope, $rootScope) {
$scope.init = function (userid, id) {
$scope.id = id;
$rootScope.userid = userid; // around the application you can get this id. no more code
};
});
或
<input ng-model="user" ng-value="$parent.userid">
或
app.controller('userIdCtrl', function ($scope) {
$scope.init = function (userid, id) {
$scope.id = id;
// $scope.userid = userid;
$scope.$broadcast('userID', userid);
};
});
app.controller('appointmentPostCtrl', function ($scope, $http) {
$scope.postData = function () {
$scope.message = "";
var error = 0;
$scope.$on('userID', function (event, data) {
$scope.userid = data;
});
});
首先从 html 标记中删除:
<input ng-model="user" ng-value="{{ userid }}">
然后尝试将 userid
包装在父控制器上的对象中:
app.controller('userIdCtrl', function ($scope) {
$scope.init = function (userid, id) {
$scope.postObj = {
id: id
userid: userid
};
};
});
然后修改子控制器如下:
if (error == 0) {
var request = $http({
method: "post",
url: "https://www.ivan-montes.com/websites/MPC_App/php/angPost.php",
data: {
user: $scope.postObj.userid, // this line should do it!
especialidad: $scope.especialidad,
facilidad: $scope.facilidad,
fecha: $scope.fecha,
hora: $scope.hora
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
/* Check whether the HTTP Request is Successfull or not. */
request.success(function (data) {
$scope.message = "" + data;
});
} else {
$scope.message = "You have Filled Wrong Details! Error: " + error;
};
};
所以我正在制作一个移动网络应用程序,我有一个表格,他们可以在其中进行约会并将其保存到 MySQL 服务器中。我需要发送隐藏的用户 ID。我已经在 ng-init 中有了它。我有一个控制器可以获取它,它可以用来把它放在 html 中。我不太擅长 angularjs 在 all.I 主要使用 php 来处理 javascript 中的所有事情和很少的事情。任何帮助将不胜感激。
这是我的表格。
<div class="login-form" ng-controller="userIdCtrl" ng-init="init('<? echo $usernameid; ?>','<? echo $usernameid; ?>')">
<div class="center" style="margin:5px;color:white;">
<h2>Registro</h2>
<form role="form" name="registro" ng-controller="appointmentPostCtrl">
<div class="form-group">
<div class="col-md-9 col-sm-9 col-xs-9">
<select ng-model="especialidad" class="styled-select" required>
<option value="">Especilidad</option>
<?php echo $especialidades ?>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-9 col-sm-9 col-xs-9">
<select ng-model="facilidad" class="styled-select" required>
<option value="">Facilidad</option>
<?php echo $facilidades ?>
</select>
</div>
</div>
<br>
{{ userid }} <!--- test that it works ----->>>
<input ng-model="user" ng-value="{{ userid }}"> <!--- actual part that doesn't works it returned empty----->>>
<input type="date" ng-model="fecha" class="styled-select" style="width:40%; float:left;">
<input type="time" id="timeInput" name="timeInput" ng-model="hora" placeholder="HH:mm:ss" min="08:00:00" max="17:00:00" required />
<br><br>
<div style="margin-top:50px;">
<button ng-click="postData()" class="button button--large " style="background-color:#6CBA45;">Registrar</button>
<ons-button modifier="quiet" class="forgot-password" ><a style="text-decoration:none;color:white;" href="login.php">Cancelar</a></ons-button>
<span id="message">{{ message }}</span>
</div>
</form>
</div>
</div>
控制器一个用于用户 ID,另一个用于处理 post 到 mysql
////////////////// userIdCtrl Controller //////////////////////
app.controller('userIdCtrl', function ($scope) {
$scope.init = function (userid, id) {
$scope.id = id;
$scope.userid = userid;
};
});
//////////// appointment post Controller/////////////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////// ////
app.controller('appointmentPostCtrl', function ($scope, $http) {
$scope.postData = function () {
$scope.message = "";
var error = 0;
/*---------------- this is the part that is no working return as empty------ */
if ($scope.user == "" || $scope.user == null) {
error = 1;
}
/*--------------------------------------------------------------------- */
if ($scope.especialidad == "" || $scope.especialidad == null) {
error = 2;
}
if ($scope.facilidad == "" || $scope.facilidad == null) {
error = 3;
}
if ($scope.fecha == "" || $scope.fecha == null) {
error = 4;
}
if ($scope.hora == "" || $scope.hora == null) {
error = 5;
}
/*---- Data is validated ------ */
if (error == 0) {
var request = $http({
method: "post",
url: "https://www.xxxxxxxx.com/angPost.php",
data: {
user: $scope.user,
especialidad: $scope.especialidad,
facilidad: $scope.facilidad,
fecha: $scope.fecha,
hora: $scope.hora
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
/* Check whether the HTTP Request is Successfull or not. */
request.success(function (data) {
$scope.message = "" + data;
});
} else {
$scope.message = "You have Filled Wrong Details! Error: " + error;
};
};
});
我是不是在 angular 的逻辑上做错了什么,或者我缺少资源?你试过只制作一个控制器,但也不起作用。这是我能得到的最接近错误句柄的信息。我想我需要一条 ng 路线或者一项服务?需要一些光。谢谢。
使用这个 ng-value="userid"
。 ng-value.
app.controller('userIdCtrl', function ($scope, $rootScope) {
$scope.init = function (userid, id) {
$scope.id = id;
$rootScope.userid = userid; // around the application you can get this id. no more code
};
});
或
<input ng-model="user" ng-value="$parent.userid">
或
app.controller('userIdCtrl', function ($scope) {
$scope.init = function (userid, id) {
$scope.id = id;
// $scope.userid = userid;
$scope.$broadcast('userID', userid);
};
});
app.controller('appointmentPostCtrl', function ($scope, $http) {
$scope.postData = function () {
$scope.message = "";
var error = 0;
$scope.$on('userID', function (event, data) {
$scope.userid = data;
});
});
首先从 html 标记中删除:
<input ng-model="user" ng-value="{{ userid }}">
然后尝试将 userid
包装在父控制器上的对象中:
app.controller('userIdCtrl', function ($scope) {
$scope.init = function (userid, id) {
$scope.postObj = {
id: id
userid: userid
};
};
});
然后修改子控制器如下:
if (error == 0) {
var request = $http({
method: "post",
url: "https://www.ivan-montes.com/websites/MPC_App/php/angPost.php",
data: {
user: $scope.postObj.userid, // this line should do it!
especialidad: $scope.especialidad,
facilidad: $scope.facilidad,
fecha: $scope.fecha,
hora: $scope.hora
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
/* Check whether the HTTP Request is Successfull or not. */
request.success(function (data) {
$scope.message = "" + data;
});
} else {
$scope.message = "You have Filled Wrong Details! Error: " + error;
};
};