I use this directive:
function communicationCreateFormDirective($timeout) {
var directive = {
restrict: 'E',
scope: {
selectedCommunicationGroups: '=',
currentUser: '='
},
templateUrl: 'app/communication/....html',
link: function($scope) {
...
connect();
and I will initially invoke the function connect() but I get the error:
angular.js:14110 ReferenceError: connect is not defined
Also $scope.connect() does not work. Does anyone know how to do?
Thats the function:
$scope.connect = function() {
var socket = new SockJS('/api/chat');
$scope.stompClient = Stomp.over(socket);
$scope.stompClient.connect({}, function(frame) {
console.log('webSocket Connected in communicationCreateForm.directive.js: ' + frame);
$scope.stompClient.subscribe('/topic/communication/' + $scope.currentUser.id, function(message) {
//vm.showMessage(JSON.parse(message.body));
alert('Message response QUMA: ' + JSON.parse(message.body));
});
});
}
$scope.connect
function defined outside of the directive scope (In another controller?) Then you should bind it to the directive by changing itsscope: { ... }
object, and add:connect: '&'
. Then you should be able to invoke the function from the directive using$scope.connect()
(But don't forget to bind it to the directive in the view using aconnect="connect()"
attribute!)