我有一个控制器,将通过管理不同的数据具有相同的功能。我是angularJS的新手,所以我不确定如何继续。我已经读过关于服务和工厂的内容,但只看到了将不同控制器中的数据注入到同一控制器中的例子。任何帮助指出我在正确的方向是值得赞赏的。注入不同的数据到相同的控制器
angular.module("myApp")
.controller("AirlineController", function() {
this.Airlines = getAirlines(); //some service call that will be ajax eventually
});
angular.module("myApp")
.controller("CitiesController", function() {
this.Cities = getCities();//some service call that will be ajax eventually
});
angular.module("myApp")
.controller("GenericController", function() {
$('.selected-items-box').bind('click', function (e) {
e.stopPropagation();
$('.image-select-wrapper .list').toggle('slideDown');
});
$(document).bind('click', function() {
$('.image-select-wrapper .list').slideUp();
});
this.ListObject = getAirlines();//this list should be populated from one of the other controllers
this.toggleSelected = function (selectedItem) {
angular.forEach(this.ListObject, function (ListItem) {
ListItem == selectedItem ? ListItem.selected = true : ListItem.selected = false;
});
};
this.getSelectedItem = function (item) {
return item.selected;
};
});
为什么你不使用'.factory'来返回你的数据,然后从我的理解中调用你的控制器 –
如果我使用工厂,我必须声明控制器中的数据,这意味着它是仍然硬编码,并且不允许在GenericController中使用动态数据。如果不是这种情况,我为我的无知道歉,正如我所提到的,我是新角色。 – aemorales1
这里如果你想要数据动态数据,那么你会得到你的工厂数据,然后返回它,并通过在控制器中调用它可以使用它 –