我有两个控制器,希望他们在不创建工厂的情况下将数据从第一个发送到第二个。
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if !AppSession.currentSession().isLoggedIn() {
presentLoginViewController(false, completion: { ()->Void in
self.removeSplash()
})
}
else {
removeSplash()
}
// some code...
}
private func removeSplash() {
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.removeSplashView()
}
}
但似乎没有用。错误是:未捕获(承诺)错误:[$ injector:unpr]
可以请任何人帮忙吗?
答案 0 :(得分:1)
在AngularJS中,controller
无法注入其他controller
。如果您想在控制器之间进行互连/共享,那么您应该使用工厂或服务。
在您的情况下,您可以
module.controller('UpdatesController', ['$scope', 'TestFactory', function ($scope, TestFactory) {
var update = {};
update.items = [{
title: 'Company goes high',
date: '24-11-2015',
excerpt: 'Labore et dolore magna aliqua',
desc: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
picture: 'images/01.jpg',
}];
$scope.items = update.items;
TestFactory.setItems($scope.items);
$scope.showUpdate = function (index) {
var selectedItem = update.items[index];
update.selectedItem = selectedItem;
$scope.navi.pushPage('update.html', {
title: selectedItem.title,
date: selectedItem.date,
excerpt: selectedItem.excerpt,
desc: selectedItem.desc,
picture: selectedItem.picture
});
};
}]);
module.controller('UpdateController', ['$scope', 'TestFactory', function ($scope, TestFactory) {
$scope.item = TestFactory.getItems();
}]);
module.factory('TestFactory', function(){
return {
getItems : function(){
return this.items;
},
setItems : function(items){
return this.items = items;
}
}
});