有没有办法在事件中访问变量集?
代码:
$scope.$on('event_detail', function (event, args) {
$scope.id = args;
console.log($scope.id); // it works
});
console.log($scope.id); // it says undefined
如果我尝试在控制台" $ scope.id"中显示,控制台会显示" undefined"。 我想从函数事件$ scope。$ on
中访问变量我的广播:
$scope.showDetail = function (data) {
$rootScope.$broadcast("event_detail", data.id_case);
$location.path("/detailcase/" + data.id_case);
};
答案 0 :(得分:4)
问题是$scope.$on
是异步的,所以当你在事件函数之外调用console.log
时,$scope.id
尚未设置。
所以你必须在$on
函数中做任何你想做的事情,因为你确定已经填充了$scope.id
;
答案 1 :(得分:0)
问题是你在这里定义{
"my_type": {
"properties": {
"title": {
"type": "string",
"fields": {
"shingles": {
"type": "string",
"analyzer": "my_shingle_analyzer"
}
}
}
}
}
..直到现在才发生呼叫..
delegate
如果您的代码看起来像下面的内容,那么它将起作用......
$scope.$on('event_detail', function (event, args) {
$scope.id = args;
// it will work because the $scope.id get populated
console.log($scope.id);
});
//$scope.id It is not populated yet ..
//it will be populated when the broadcast get called
console.log($scope.id);