Angularjs integration with jquery plugins - angularjs

I need to integrate angularjs with galleria or supersize image galleries plugin.
My purpose to communicate with server on sliding of the image and the problem is that galleria has its own callbacks on different actions like on image change
Galleria.on('image', function(e) {
Galleria.log(this); // the gallery scope
});
And the problem is that how the angular js controller functions will be notified on the image change.
Newbie in angular so may be this is silly question but tried a lot to figure it out but was unable to find any thing.
thanks

If this within the callback is a DOM element you can access the scope from the controller that the element resides in using:
var controllerScope= angular.element(this).scope();
/* update a variable in scope*/
controllerScope.someVar=this.src;
/* use $apply() so angular reacts to update*/
controllerScope.$apply();
DEMO (using jQuery cycle plugin)
http://jsfiddle.net/RUTN5/

Related

Angular Hybrid Application, Upgraded Component with Template Function not receiving $element or $attrs

As the title says, I am working in an Angular Hybrid app. I have an old AngularJS component that uses a template function to dynamically build itself based on the inputs passed in to it.
I've upgraded that component to be useable from Angular, however the template function for it no longer works because $element and $attrs aren't injected correctly in to the template function.
Here's a simple stackblitz showing the issue
As you can see, when using the component directly from AngularJS, it is given the $element and $attrs values correctly, but when rendering it as an upgraded component from Angular, it doesn't. Is there some way I'm supposed to provide those manually for the upgraded component?
Turns out this is currently a limitation of upgraded component template functions. I opened a bug on Angular's github here and received that response. I've opened a PR here to somewhat address the issue as best as possible.

Is it possible to pass information from an Angular component to an AngularJS component?

I am working on a hybrid app, it's running a main Angular (7) component and within it has an upgraded (through ngUpgrade) AngularJS component running a Gantt chart. I am trying to pass the data it needs from the Angular component's controller to the AngularJS one, but I'm failing to find resources on this topic, which makes me think it is not possible. Anyhow, I would like to ask just in case anyone has ever tried this.
Thank you in advance!
You can use global object, like window. Add to this object properties chanel like RxJS/BehaviourSubject. Remember import RxJs library.
In index.html add script where define chanel
<body>
<script>
window['myChanel'] = new BehaviourSubject()
</script>
</body>
In AngularJS app, where you need, set Observer:
let transData;
window.myChanel.pipe(tap(value => transData = value ))).subscribe()
In Angular app, use chanel for emit value
window.myChanel.emit('myTestValue')

Why isn't Angular's `$location.url` working after my Bootstrap Modal close?

Problem:
Anyone know why in my cb.register function below, my Modal window closes OK, but the $location.url('/dashboard') does not properly run?
I don't get any console errors, and the page does not redirect. However, console.log('TRANSITION DONE') still prints out in the code below after the modal window closes, so I know the event is being recognized:
Code:
app.controller('userController', ['$scope', 'userFactory', '$location', function($scope, userFactory, $location) {
var cb = {
register: function(createdUser) {
// Closes Modal window:
$('#myModal').modal('hide');
// Runs after close with completed transition effects:
$('#myModal').on('hidden.bs.modal', function (e) {
console.log('TRANSITION DONE');
$location.url('/dashboard');
})
},
};
}]);
Questions:
Why does the $location service not behave properly when nested inside of my .on() function?
Should I be using angular-ui instead? (Of which I was not familiar until researching solutions to this issue, and instead started my project by framing out all the views first using bootstrap3 and including the JS files, etc).
What I've Tried:
I've tried passing the $location parameter into the .on()'s callback function, along with e, but that made no real change and the function should still have access to $location in the larger scope.
Console logging $location or $location.url works properly, so I know the methods are accessible in the scope.
Moving the $location function outside of the on() function does load the dashboard page, however the fade-out modal close does not complete. A light-grey transparent BG is overlaying my dashboard page, which is why I chose to utilize the hidden.bs.modal event per the documentation: http://getbootstrap.com/javascript/#modals
// Closes window:
$('#myModal').modal('hide');
// Redirects to page but fade-out on Modal not completed:
$location.url('/dashboard');
Desired Behavior/Pseudo-Code:
Modal window closes (when callback runs)
After Modal fade out:
$location.url('/dashboard') runs, loading dashboard page
Thank you to any angular/bootstrap ninjas whom can help!
Why does the $location service not behave properly when nested inside of my .on() function?
You're directly using Bootstrap modals in your application -- which is OK. But, this requires you to manipulate the modal with jQuery. It is not advisable to use jQuery in an AngularJS application as AngularJS has its own methods of DOM manipulation and therefore you shouldn't manually manipulate the DOM via jQuery.
To answer your question, since you're using jQuery to directly manipulate the DOM, it is likely that AngularJS' digest cycle has not picked up on the modal closing. You could get around this by telling AngularJS to activate a digest cycle via $scope.$apply(). However, this will easily dirty up your code and just doesn't make sense according to best AngularJS principles.
Should I be using angular-ui instead? (Of which I was not familiar until researching solutions to this issue, and instead started my project by framing out all the views first using bootstrap3 and including the JS files, etc).
Yes. I would highly suggest using angular-ui. As per the documentation:
This repository contains a set of native AngularJS directives based on Bootstrap's markup and CSS. As a result no dependency on jQuery or Bootstrap's JavaScript is required.
As you can see, all of the Bootstrap functionality has been wrapped up into AngularJS directives that can easily be dropped into your application without worrying about jQuery.

can I use EJS with AngularJS?

Hi I am new to AngularJS. I have great web app already running with JQuery and jQuery UI.
Now I want to completely get rid of JQuery and am going to migrate to Angularjs because of its MVC (MVW) pattern.
So my jQuery application is running with EJS for templates and entirely DOM manipulation. But when I think about Angular js, I have doubts. Can I still use EJS or not?
So please guide me whether I can use or not.
Another doubt is, let's assume I have list page. It is updated dynamically, and it will display 10 records first then based on user scroll, the next 10 records will be appended in the DOM after AJAX. Can we append to the DOM dynamically using Angular?
How do I achieve these kind of things in Angular?
You can use EJS (server or client side) in combination with Angular but there's no need and you'll probably overcomplicate things. AngularJS is very capable for manipulating the DOM by itself in a very separation of concerns kind of way. The most elegant way to work with Angular is to have a RESTful backend and just serve some static html/js files from a webserver.
As for endless scrolling, there are tons of ready to use plugins (modules) to choose from or you can write your own. Basically this will need a view with a ng-repeat directive to display the currently loaded items and a directive that will notify a controller/scope to load more items when the user is scrolling down.
A nice simple article can be found here.
Similar questions:
Mixing angular and asp.net mvc webapi
Actual use of jade template and angularjs
Yes of course you can use EJS with Angular JS. You might want to have a look at this;
https://gist.github.com/jrmoran/4277025
And about your DOM manipulation question. Yes you can append DOM dynamically using Angular JS. Angular JS have a strong feature of two way data binding which dynamically updates its DOM content when a model variable changes.
Have a look at this:
http://docs.angularjs.org/guide/databinding

How to initialize controller manually after app has bootstrapped?

I trying to add angular widgets into an existing non-angular app. The problem is that these widgets are not added by Angular, and are mostly injected into the DOM by a non-angular script.
The first thing I thought was to use angular.bootstrap for every widget. But the issue with that approach is that I cannot share services between widgets. So, instead of doing that, I want to bootstrap the Angular app once, and load the controllers manually for each widget.
Here is a sandbox jsbin where I've been trying stuff without success
I'm trying to make app1 manually and app2 automatically, while sharing the sharedThing service.
Well, I found my answer.
I don't know if I had solved it before, but I had to make a call to $apply after recompiling the element to see the changes without manually modifying the input. Also, I discovered that I can initialize the angular app with an element outside the DOM and I can compile elements outside the root element! That is perfect for what I'm trying to achieve.
Anyway, here is the jsBin with the code working.
In the app config
app.config(function($controllerProvider) {
app.register =
{
controller: $controllerProvider.register
};
}
then after you declare controller function
app.register.controller('CtrlName',CntrlFn]);
Dan had wrote a great article about it. http://weblogs.asp.net/dwahlin/archive/2013/05/22/dynamically-loading-controllers-and-views-with-angularjs-and-requirejs.aspx

Resources