Angularjs 如何让角带模式/侧边触发事件?
我有一个控制器:Angularjs 如何让角带模式/侧边触发事件?,angularjs,angular-strap,Angularjs,Angular Strap,我有一个控制器: angular.module('mean').controller('LocationController', [ '$scope', '$location', '$rootScope', '$aside', '$routeParams', '$filter', 'ngTableParams', 'LocationService', 'UserService', 'CompanyService', function ($scope, $location, $root
angular.module('mean').controller('LocationController', [
'$scope', '$location', '$rootScope', '$aside', '$routeParams', '$filter',
'ngTableParams', 'LocationService', 'UserService', 'CompanyService',
function ($scope, $location, $rootScope, $aside, $routeParams, $filter,
ngTableParams, LocationService, UserService, CompanyService) {
$rootScope.menuItem = 'locations';
$scope.contentTemplate = '/views/location/index.html';
$scope.locations = [];
$scope.current_location = null;
$scope.newLocation = {};
$scope.location_parent_id = $routeParams.location_parent_id;
$scope.validation_errors = [];
$scope.index = function() {
CompanyService.initialized.then(function() {
var company_id = CompanyService.getCompany()._id;
LocationService.list(company_id, $routeParams.location_parent_id).then(function(response) {
if(response.data.status === 'ok') {
$scope.locations = response.data.locations;
$scope.current_location = response.data.location || null;
} else {
alert('TBD');
}
});
});
$scope.tableParams = new ngTableParams({
page: 1,
count: 10,
}, {
total: $scope.locations.length,
getData: function($defer, params) {
var orderedData = params.sorting() ? $filter('orderBy')($scope.locations, params.orderBy()) : $scope.locations;
orderedData = params.filter() ? $filter('filter')(orderedData, params.filter()) : orderedData;
$scope.locations = orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count());
params.total(orderedData.length);
$defer.resolve($scope.locations);
}
});
}
$scope.addLocationModal = function() {
$scope.location_types = ['warehouse', 'section', 'row', 'shelf', 'bin'];
$aside({
scope: $scope,
template: '/views/location/addLocationAside.html',
show: true
});
}
$scope.createLocation = function() {
$scope.newLocation.company_id = CompanyService.getCompany()._id;
LocationService.create($scope.newLocation).then(function(response) {
if(response.data.status === 'ok') {
$scope.$hide();
$scope.index();
} else {
$scope.validation_errors = response.data.error;
}
});
}
}
]);
在模式中,我有一个表单,提交时调用
createLocation
函数。如果位置创建成功,我希望模式关闭,索引运行并重新更新列表。但这似乎没有发生。我认为这是一个$scope
问题,但我不确定是什么问题。没有更多细节,很难说出问题所在。下面是一些猜测:
适用范围
如果LocationService
不处理$scope.$apply
,您可能需要在createLocation
内的回调中调用它,更改才能生效。例如:
$scope.createLocation = function() {
$scope.newLocation.company_id = CompanyService.getCompany()._id;
LocationService.create($scope.newLocation).then(function(response) {
$scope.$apply(function () {
if (response.data.status === 'ok') {
$scope.$hide();
$scope.index();
} else {
$scope.validation_errors = response.data.error;
}
});
});
};
您可能还需要在LocationService.list
回调中的$scope.index
方法中执行此操作
初始化只解析一次
有可能在加载应用程序时,CompanyService.initialized
承诺只解析一次,因此在关闭模式后,您实际上没有更新$scope.locations
。我想换成:
var isCountryServiceInitalized = false;
$scope.index = function() {
function updateLocations() {
var company_id = CompanyService.getCompany()._id;
LocationService.list(company_id, $routeParams.location_parent_id).then(function(response) {
if (response.data.status === 'ok') {
$scope.$apply(function () { // you don't need this if LocationService already calls $scope.$apply
$scope.locations = response.data.locations;
$scope.current_location = response.data.location || null;
});
} else {
alert('TBD');
}
});
}
if (!isCountryServiceInitalized) {
CompanyService.initialized.then(function () {
isCountryServiceInitalized = true;
updateLocations();
});
} else {
updateLocations();
}
...
};
ngTable重新加载
看起来您正在使用ng表。您可能需要在更新$scope.locations
后重新加载表。使用:
$scope.tableParams.reload();
一把小提琴就好了。还有,这个失败在哪里,createLocation调用了吗?你的承诺怎么样?什么是$scope.$hide?它实际上并没有失败-它只是没有重做$scope.index()并更新ItemsOrry,您的意思是它没有被调用还是被调用?如果是,该功能中的所有承诺是否都已解决?