Get vis.js networkEvents from the controller - angularjs

I need to use click event in my controller.
The networkEvents are created in angular-vs.js directive and I need it in my controller to trigger the click event of individual nodes.
This is a code snippet I'm using but it does seem to work:
$scope.networkEvents = { onload:function(network){ alert("Clicked"); } }
Thanks,
Here is my plunker.

index.html
It looks like you don't set your events attribute of the vis-network directive, so I think you meant:
<vis-network data="data" options="options" events="networkEvents" height="100%"></vis-network>
angular-vis.js
Also, in the onload callback of the vis-network directive (line 151) you're passing graph into the onload function, but it isn't defined. (Maybe it's supposed to be network instead?):
// onLoad callback
if (scope.events != null && scope.events.onload != null &&
angular.isFunction(scope.events.onload)) {
scope.events.onload(network);
}
script.js
With the above changes, the onload function seems to work as expected. Since you've already set up the forEach loop to attach the events, to get the click event you just need to add it to the networkEvents object:
$scope.networkEvents =
{
onload: function(network){
alert("I'm loaded");
},
click: function(clicked) {
alert("id: " + clicked.nodes[0] + " was clicked");
}
}

Related

How to call a method in the child window in Angular JS

My angular JS application has a view button. When clicking on the view button, it will call method and that method changes the content in the same page. Now I have a request that when clicking on the view button, it has to open in a new tab and display the contents.
Previous code:
HTML:
<md-button ng-click="ctrl.ViewClick(item)">View</md-button>
Controller:
vm.ViewClick = function(item) {
// The browser URL is same but it is a new page with the new contents.
// Calls a angular js service and loads the contents
}
Now, I need to call that function in new browser tab.
I made the following changes but didn't worked. Can you please help me on this.
HTML:
<md-button ng-click="ctrl.NewTabClick(item)">View</md-button>
Controller:
vm.newTabClick = function(item){
$Window.open($location.absURL(), 'blank');
// How do I call vm.ViewClick function after opening the new browser window?
};
This is the old angular JS. Thanks for helping on this.
On workaround you can do in this case is, While opening the page pass query parameter in the URL.
vm.newTabClick = function(item){
// Add `viewLink=true` query parameter with true value
$Window.open($location.absURL() + '&viewLink=true', 'blank');
// Store item in sessionStorage
window.sessionStorage.setItem('item', JSON.stringify(item));
}
And then from the component where you want to call the parameter. You can write below code on $onInit lifecycle hook.
vm.$onInit = function () {
// Inject $routeParams in dependency areay
const storedItem = sessionStorage.getItem('item')
if ($routeParams.viewLink == 'true' && storedItem && storedItem != 'null') {
vm.viewLink(JSON.parse(storedItem));
}
}

Appending ngBindHtml function using .attr in link function not working

A am attempting to attach the ngBindHtml directive in an application within a link function of a directive. The module in which the directive is located injects ngSanitize like such:
angular.module('ui.bootstrap.contextMenu', ['ngSanitize'])
.directive('contextMenu', cm);
where cm is the directive function. The link function looks like:
var link = function ($scope, element, attrs) {
element.on('contextmenu', function (event) {
event.stopPropagation();
$scope.$apply(function () {
event.preventDefault();
var options = $scope.$eval(attrs.contextMenu);
var model = $scope.$eval(attrs.model);
if (options instanceof Array) {
if (options.length === 0) {
return;
}
renderContextMenu($scope, event, options, model);
} else {
throw '"' + attrs.contextMenu + '" not an array';
}
});
});
};
where renderContextMenu sketches out the html that will be attached to the body of the page. Within this function I have the following lines of code:
$div.attr('ng-bind-html', text);
$a.append($div);
which should produce something that looks like:
<a><div ng-bind-html="the text"></div></a>
and it does. The problem is that the text is not actually displayed. Does anyone have any thoughts on this?
I think the "angular-y" way of doing this is to put the html code for the context menu that you're hoping to bind directly into the template, and just hide/show it as appropriate: only show it if a valid contextmenu event occurs and has options.length > 0.

Animation End event Zurb Foundation for apps

I have a scroll up event and a scroll down event. I am trying to use FoundationAPI.animate to slideInUp a div on scroll up and then slideOutBottom on scroll down. The original state of the div has a class of hide because without scrolling - it lives off the bottom of the page. I have this working except for one problem. When the slideOutBottom animation is finished, the hide class no longer is in the div, so it shows after the slideOutBottom completes. I want it to stay hidden like it was at state 0.
Code:
$scope.scrollUp = function() {
if ($scope.lock == false ){
console.log('scrolling up');
$scope.lock = true;
FoundationApi.animate($('.footer-bar'), $state, "slideInUp", "hide");
console.log($state);
}
};
$scope.scrollDown = function() {
if ($scope.lock == true){
console.log('scrolling down');
$scope.lock = false;
FoundationApi.animate($('.footer-bar'), !$state, "hide", "slideOutBottom");
// setTimeout(function() {
// $('.footer-bar').addClass('hide');
// }, 999);
}
};
How do I access a callback for the FoundationAPI.animate(4) function such that it fires when it is complete? The commented out timeout works, but after the slideOutBottom finishes, the footer-bar appears, then the hide class gets applied. This causes the div to blink quickly. Anyone? The documentation on FoundationApi is lacking right now...
Not an answer but a solution. This a more natural AngularJS approach:
Controller:
function myController($scope, ...) {
$scope.lock = false;
$scope.scrollUp = function() {
if ($scope.lock == false ){
$scope.lock = true;
$scope.$apply();
}
};
$scope.scrollDown = function() {
if ($scope.lock == true){
$scope.lock = false;
$scope.$apply();
}
};
}
View:
<a id="footer" class="slideInUp slideOutBottom footer-bar" ng-hide="lock" zf-open="myModal">
<div>Click Me</div>
</a>
So the scroll events I bind with a directive on the particular view container; this determines the logic for when we detect the scroll up/down events. From the directive I call these functions that are defined the scoped controller (above). These are bound 2-ways with the "lock" variable accessible in this scope.
Note that the way the animations are triggered are with the classes in the anchor. I think there are defined enter animations and defined exit animations which are both present in the class. So ng-hide kicks in depending on the boolean value of the $scope.lock variable in my controller.
Totally not clear - I guess would be more clear to an Angular expert

angularjs ng-click getting info from child element

I am trying to get info from the ng-click element. When I log $event from clicked element I get the right info but when I click the child element of then I get the info about the child and not the parent where the ng-click is set. Here is the fiddle link
http://jsfiddle.net/g3x2ndyc/5/.
var app = angular.module('app',[])
app.controller('appCtr', function($scope) {
$scope.testThis = function(evt){
//evt.stopPropagation();
// evt.preventDefault();
console.log(evt.srcElement.offsetLeft)
console.log(evt.srcElement.offsetWidth)
console.log('____________________________')
}
});
use evt.currentTarget not evt.srcElement (that one is mainly for IE).
Look at fiddle http://jsfiddle.net/g3x2ndyc/11/
var app = angular.module('app',[])
app.controller('appCtr', function($scope) {
$scope.testThis = function(evt){
console.log(evt.currentTarget.offsetLeft)
console.log(evt.currentTarget.offsetWidth)
console.log('____________________________')
}
});
Hard to tell exactly what you are ultimately trying to do, but you can just pass the id in to the click event then grab the position based on that.
ng-click="myevent('idName')
myevent(id) {
jquery('#'+id).position();
}

Listen for a specific $includeContentLoaded

$includeContentLoaded is fired every time ngInclude is updated.
$scope.$on('$includeContentLoaded', function() {
// Emitted every time the ngInclude content is reloaded
});
What I would like to do, is to listen for a specific ngInclude to be loaded, like a callback:
$scope.includePath('/path/to/iclude', function() {
// Fired when the includePath ngInclude have finished loading
}
Is this possible?
You can use "onload" tag:
<div ng-include="url" onload="test(url)">
</div>
And check url in scope function, for example:
$scope.test = function(url){
switch(url) {
case '/path/to/iclude':
/*******YOUR CODE***********/
break;
}
}
in Angular 1.3, you have:
$rootScope.$on('$includeContentLoaded',function(event,url){
if (url == 'YourDesiredPath') {
// do something
}
});
Angular has its own method : $includeContentLoaded
$scope.$on('$includeContentLoaded', function(eve,uri) {
if(uri == 'views/templates/sample.html'){
// do something
});
}

Resources