我对angularjs很新。 我在我的spinner中有类别列表和产品列表。类别和产品是我的数据库中的两个不同的表作为category_insert和product_insert respiectively.Now当我点击注册类别和产品必须根据“userid”发布到他们各自的表。但是产品是基于categoryids.How我能做到吗?TIA 角度代码:
(function () {
var app = angular.module('MyApp');
app.controller('ProductCtrl', function($scope, $location, $http, ProductService) {
$scope.productregstr = function() {
$http ({
method : 'POST',
url: 'http://localhost:3000/onio/v1/categoriesinsert',
data: $scope.category_insert
});
// $auth.productregstr($scope.category_insert)
// .then(function(response) {
// toastr.info('You have successfully created a new account, Please Login');
// })
// .catch(function(response) {
// toastr.error(response.data.message);
// });
};
$scope.category_insert = {
categoryname: '',
categorytype: ''
};
$scope.categories = ProductService.getCategory();
$scope.getCategoryProducts = function() {
$scope.products = ProductService.getCategoryProducts($scope.category_insert.categoryname);
};
});
app.factory("ProductService", ['$filter', function($filter) {
this.categoryList = [
{ "id": 1, "category": "ALL MOVIES, TV SHOWS & MUSIC" }//india:1
];
this.productList = [
{"Id":1, "product":" All Movies and TV Shows", "categoryId": 1},
{"Id":2, "product":" Video Games", "categoryId": 1},
{"Id":3, "product":" All Music", "categoryId": 1}
];
this.getCategory = function() {
return this.categoryList;
};
this.getCategoryProducts = function(categoryId) {
var products = ($filter('filter')(this.productList, {
categoryId: categoryId
},true));
return products;
};
return this;
}]);
})();
Angular code:
<div class="container login-container">
<div class="panel">
<div class="panel-body">
<div ng-if="messages.error" role="alert" class="alert alert-danger">
<div ng-repeat="error in messages.error">{{error.msg}}</div>
</div>
<form name="prdctRegister" method="POST" ng-submit="productregstr()">
<div class="form-group">
<label for="category" class="col-sm-2 control-label">Category </label>
<select ng-model="category_insert.categoryname"
ng-options="obj.id as obj.category for obj in categories"
ng-change="getCategoryProducts()"
class="form-control"
ng-required="true"
id="category">
<option value="">-- Choose Category --</option>
</select>
</div>
<div class="form-group">
<label for="product" class="col-sm-2 control-label">Product </label>
<select ng-model="product"
ng-options="x.Id as x.product for x in products"
class="form-control"
ng-required="true"
id="product">
<option value="">-- Choose Product --</option>
</select>
</div>
<button type="submit" class="btn btn-success" ng-disabled="frmRegister.$invalid">Register</button>
</form>
</div>
</div>
</div>