AngularJS 프런트 엔드로 Electron (node.js) 앱을 작성 중입니다. OS 수준에서 실행되는 node.js와 클라이언트 수준에서 실행되는 Angular로 인해 두 사람은 IPC (또는이 경우 https://github.com/develephant/electangular에서 구현 한 ipc)를 사용하여 서로 통신 합니다.
이 경우 노드 앱에 IPC 메시지를 보내는 데 사용되는 서비스가 있습니다.
app.service('msgService', ['ipc', '$rootScope', '$q', function(ipc, $rootScope, $q){
this.sendMsg = function(type, value) {
var message = {
type: type,
value: value
};
ipc.send(message);
};
}]);
이 서비스는 기본 컨트롤러 인 컨트롤러에 삽입되고 호출됩니다.
app.controller('mainController', ['$scope', '$rootScope', 'electron', 'msgService', 'hey', function($scope, $rootScope, electron, msgService, hey){
msgService.sendMsg('db_find', {});
// I can see the variable inside this console.log
console.log($rootScope);
// I get undefined here
console.log($rootScope.msg);
}]);
노드 앱이 요청을 생성하면 ipc를 통해 메시지를 다시 보낸 다음 Angular 앱의 .run 섹션에있는 코드 조각에 의해 선택됩니다.
app.run(['$window', '$rootScope', function($window, $rootScope) {
$rootScope.$on('electron-msg', (event, msg) => {
$rootScope.msg = msg;
});
}]);
위의 코드는 실행중인 $ rootScope 변수에 msg 속성을 설정 한 다음 컨트롤러 내부에서 액세스 할 수있을 것으로 예상합니다. console.log 줄의 주석에서 $ rootScope에 액세스 할 수 있고 값이 반환되는 것을 알 수 있습니다. 그러나 $ rootScope.msg를 개별적으로 기록하면 'undefined'가됩니다. $ rootScope.msg 변수에서 변수를 설정하려는 경우에도 마찬가지입니다. 정의되지 않았습니다.
나는 이것으로 모든 것을 시도했고, 나는 완전히 내 테더의 끝입니다!
많은 도움을 주셔서 감사합니다 :)
편집하다:
다음은 console.log ($ rootScope); 요청대로 :
Scope {$id: 1, $$childTail: ChildScope, $$childHead: ChildScope, $$prevSibling: null, $$nextSibling: null…}
$$ChildScope
:
ChildScope()
$$applyAsyncQueue
:
Array[0]
$$asyncQueue
:
Array[0]
$$childHead
:
Scope
$$childTail
:
Scope
$$destroyed
:
false
$$isolateBindings
:
null
$$listenerCount
:
Object
$$listeners
:
Object
$$nextSibling
:
null
$$phase
:
null
$$postDigestQueue
:
Array[0]
$$prevSibling
:
null
$$watchers
:
Array[2]
$$watchersCount
:
99
$id
:
1
$parent
:
null
$root
:
Scope
msg
:
Array[4]
notificationCount
:
0
notifications
:
Array[0]
online
:
true
__proto__
:
Object
잘못된 형식에 대해 사과드립니다! 그러나 $ rootScope 안에있는 메시지를 볼 수 있습니다!
전자 라이브러리가 이러한 메시지를 비동기 적으로 실행하는 것으로 $rootScope.msg
보이므로 로깅을 수행 할 때 아직 채워지지 않습니다.
물론 컨트롤러에서 이벤트를들을 수 있습니다.
msgService.sendMsg('db_find', {});
$rootScope.$on("electron-msg", (event, msg) => {
// message done
console.log(msg);
});
물론 이것은 ipc 서비스를 통해 전송되는 다른 메시지도 포착합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다