我的目标是将一些数据从角度控制器发送到另一个。
以下是必须发送数据的控制器:
myApp.controller('MapCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.loadData = function () {
$http.get('/map/GetListDB').success(function (data, status, headers, config) {
//Logic here is working fine, it creates a table named "ExcelCols" which is a table of strings
$scope.$broadcast("SET_EXCEL_TITLES", $scope.ExcelCols);
})
}
}]);
这是第二个控制器
myApp.controller('ExcelViewCtrl', ['$scope', '$http', function($scope, $http) {
$scope.$on("SET_EXCEL_TITLES", function (event, excelCols) {
//this event is never fired
$scope.ExcelCols = excelCols;
});
}]);
我的观点是这样设计的:
<body ng-app="myApp">
<div ng-controller="MapCtrl">
//everything OK here
</div>
<div ng-controller="ExcelViewCtrl">
<table>
<thead>
<tr>
<th ng-repeat="col in ExcelCols">{{col}}</th>
</tr>
</thead>
</table>
</div>
</body>
答案 0 :(得分:15)
根据控制器的结构,w.r.t到$broadcast
消息将被路由。
将事件名称向下调度到所有子范围(及其范围) 孩子们)通知听众的注册ng。$ rootScope.Scope#$。
这意味着发送广播的控制器应该在子控制器html的父html上定义。
根据您的html结构,使用$rootScope.$broadcast
。将$rootScope
注入MapCtrl
并在其上调用$broadcast
方法。
答案 1 :(得分:6)
我认为您需要使用$rootScope
代替$scope.$broadcast
。请参阅JSFiddle
答案 2 :(得分:3)
以下是AngularJS的示例 - 控制器之间的通信:
使用共享服务进行通信的示例。
http://jsfiddle.net/simpulton/XqDxG/
"ControllerZero" Broadcast to "ControllerOne" and "ControllerTwo"
和视频教程
http://onehungrymind.com/angularjs-communicating-between-controllers/
答案 3 :(得分:0)
另一个选择是使用$ rootScope列出事件和本地$ scope来发出它。我创建了这个plnkr来测试它http://plnkr.co/edit/LJECQZ?p=info
cntWrp
&#13;
#include <set>
#include <vector>
#include <iostream>
#include <stdexcept>
#include <type_traits>
class emptyClass
{ };
template <typename ... Ts>
struct funcType;
template <typename T, typename ... Ts>
struct funcType<T, Ts...>
{ using type
= typename std::conditional<T::result,
typename T::type, typename funcType<Ts...>::type>::type; };
template <>
struct funcType<>
{ using type = emptyClass; };
#define methodCheck_1(meth) \
\
class helpMeth_1_##meth {}; \
\
template <typename T, typename A> \
struct isWithMethod_1_##meth \
{ \
template<typename U> \
static decltype(U().meth(A())) func (U*); \
\
template<typename U> \
static emptyClass func (...); \
\
static const bool result \
= ! std::is_same<emptyClass, \
decltype(func<T>(nullptr))>::value; \
\
using type = helpMeth_1_##meth; \
}
methodCheck_1(insert);
methodCheck_1(push);
methodCheck_1(push_back);
methodCheck_1(push_front);
template <typename>
class cntWrp;
template <template <typename ...> class C, typename X, typename ... Xs>
class cntWrp< C<X, Xs...> >
{
private:
using addModeType = typename funcType<
isWithMethod_1_push_back<C<X, Xs...>, X>,
isWithMethod_1_insert<C<X, Xs...>, X>,
isWithMethod_1_push<C<X, Xs...>, X>,
isWithMethod_1_push_front<C<X, Xs...>, X>>::type;
static constexpr addModeType addMode {};
void addVal (X const & x, helpMeth_1_push_back const)
{ val.push_back(x); }
void addVal (X const & x, helpMeth_1_push const)
{ val.push(x); }
void addVal (X const & x, helpMeth_1_insert const)
{ val.insert(x); }
void addVal (X const & x, helpMeth_1_push_front const)
{ val.push_front(x); }
void addVal (X const & x, emptyClass const)
{ throw std::runtime_error("cntWr<>::addVal without mode"); }
public:
C<X, Xs...> val {};
cntWrp ()
{ }
cntWrp (C<X, Xs...> const & v0) : val { v0 }
{ }
void addVal (X const & x)
{ addVal(x, addMode); }
};
int main ()
{
cntWrp<std::set<int>> csi;
csi.addVal(2);
csi.addVal(7);
csi.addVal(5);
std::cout << "set:" << std::endl;
for ( auto const elem : csi.val )
std::cout << elem << std::endl;
cntWrp<std::vector<int>> cvi;
cvi.addVal(2);
cvi.addVal(7);
cvi.addVal(5);
std::cout << "vector:" << std::endl;
for ( auto const elem : cvi.val )
std::cout << elem << std::endl;
}
&#13;
唯一的缺点是$ rootScope将会监听,并且必须明确地将$ destroy调用它。