我是angularJs的新手,我正在开发一个app,我有以下规格..
现在我的问题是,一旦Brand被创建并且模型接近,我如何从carcontroller更新$ scope.brands以更新品牌列表以创建新车形式?
我会等待回复。
angular.module('application').controller('CarsEditCtrl',
function ($scope, $http, $window,$modal, $state, $stateParams, CarService,BrandService) {
$scope.getBrands = function () {
BrandService.getBrands().then(
function (response) {
$scope.brands = response.data;
console.log(response.data);
},
function (response) {
console.log('Error while loading the brands,', response);
});
};
$scope.getBrands();
if ($state.includes('cars.edit')) {
$scope.panelTitle = 'Edit Car';
$scope.submitButtonTitle = 'Update';
$scope.getCar($stateParams.guid);
$scope.update = function () {
var yearOfProductionDate = new Date($scope.car.yearOfProduction);
var data = {
brand: $scope.car.brand.guid, model: $scope.car.model, yearOfProduction: yearOfProductionDate
};
CarService.updateCar($stateParams.guid, data).then (
function (response) {
$scope.showSuccessAlert = true;
$scope.successAlertMessage = 'Car information is updated successfully.';
},
function (response) {
alert('Error, information is not updated please try again.');
});};
} else {
$scope.panelTitle = 'Create New Car';
$scope.submitButtonTitle = 'Save';
$scope.create = function() {
var yearOfProductionDate = new Date($scope.car.yearOfProduction);
var data = {
brand: $scope.car.brand.guid,
model: $scope.car.model
};
CarService.addCar(data).then (
function (response) {
$scope.showSuccessAlert = true;
$scope.successAlertMessage = 'Car information is saved successfully.';
$window.location = '/#/dashboard/cars/' + response.data.guid;
},
function (response) {
alert(response.data.message);
});
};
}
$scope.cancel = function () {
$window.location = '/#/dashboard/cars';
};
$scope.closeSuccessAlert = function(index) {
$scope.showSuccessAlert = false;
};
//// MODEL BRAND CREATE FORM
$scope.openCreateBrandModel = function (open) {
if (!open) {
return;
}
$scope.car.brand = '';
$modal.open({
templateUrl: 'views/pages/dashboard/brands/model-edit-form.html',
backdrop: false,
windowClass: 'modal',
controller: 'BrandsEditCtrl',
resolve: {
user: function () {
return $scope.user;
}
}
----Brands Controller---------------------
angular.module('application').controller('BrandsEditCtrl',
function ($scope,
$modalInstance,
$state,
BrandService) {
$scope.create = function() {
var data = {
name: $scope.brand.name
};
BrandService.addBrand(data).then (
function (response) {
console.log(response.data.guid);
$scope.cancel();
},
function (response) {
alert(response.data.message);
});
};
$scope.submit = function () {
console.log('Submiting user info.');
/// HERE I NEEED TO TELL CarsController to update brands list.
$modalInstance.dismiss('cancel');
}
$scope.cancel = function () {
$modalInstance.dismiss();
};
});
答案 0 :(得分:0)
$scope.brands.push(newBrandYouJustCreated);
或者您可以发送http请求以获取新品牌列表,并将其存储在范围内,就像您最初可能将品牌放在范围内一样。
The documentation解释了如何处理模态:
要知道对话框何时关闭,您需要在模态实例结果承诺上注册成功回调:
var modalInstance = $modal.open(...);
modalInstance.result.then(function(data) {
// the modal has been closed, and passed data to its opener
}
要在模态关闭时将数据传递给开启者,而不是使用$modalInstance.dismiss('cancel');
,它告诉开启者&#34;用户选择取消&#34;,而是使用< / p>
$modalInstance.close(theBrandYouHaveJustCreated);
更确切地说:&#34;用户没有选择取消。他选择继续创立品牌,而这就是他创造的品牌&#34;。