Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice for opening a jquery dialog from angular?

Heres the html:

<div ng-controller="MyCtrl">     <a ng-click="open()">Open Dialog</a>     <div id="modal-to-open" title="My Title" ui-jq="dialog" ui-options="{width: 350, autoOpen: false, modal: true}">         Dialog Text     </div> </div> 

And here's the js:

function MyCtrl($scope)  {     $scope.open = function () {         $('#modal-to-open').dialog('open');     } } 

Is this the best way to go about doing this? It seems like there could be a better way of opening it without accessing the DOM but I am not sure how I would go about that. The above code works, I am just wondering if this is the way I should go about doing this. Any input is welcome.

like image 631
testing123 Avatar asked Oct 19 '12 04:10

testing123


People also ask

Is it good practice to use jQuery in Angular?

jQuery is a small yet feature-rich powerful javascript library that helps to manipulate the HTML DOM with less javascript code. We can use jQuery with Angular. There are some situations you come across while building Angular apps that it's absolutely necessary to use a library like jQuery to get something done.

Can you mix jQuery and Angular?

You can always use jQuery and Angular in the same project and sometimes jQuery is much faster than Angular for few operations.

Does Angular use the jQuery library True or false?

Does AngularJS use the jQuery library? Yes, AngularJS can use jQuery if it's present in your app when the application is being bootstrapped. If jQuery is not present in your script path, AngularJS falls back to its own implementation of the subset of jQuery that we call jQLite.


1 Answers

"Best practice" is fuzzy ground here. If it's readable and it works, then you're 90% there, IMO, and it's probably fine.

That said, the "angular way" is to keep DOM manipulation out of the controller, and to use dependency injection to make sure everything is testable. Obviously the way you illustrated above would be hard to test, and puts some DOM manipulation in the controller.

I guess what I would do to get the DOM manipulation out of the controller is use a directive:

A simple directive to tie your dialog open call to a click on an element:

app.directive('openDialog', function(){     return {         restrict: 'A',         link: function(scope, elem, attr, ctrl) {             var dialogId = '#' + attr.openDialog;             elem.bind('click', function(e) {                 $(dialogId).dialog('open');             });         }     }; }); 

And in mark up it would be used like so:

<button open-dialog="modal-to-open">Open Dialog</button> 

Now, this is obviously very basic. You could get pretty advanced with this if you wanted to, adding additional attributes for different options in the dialog.

You could go even further and add a Service that opened the dialog for you, so you could inject it into your controller or even your directive, and get the call out of there that way. For example:

app.factory('dialogService', [function() {     return {         open: function(elementId) {             $(elementId).dialog('open');         }     }; }]); 

And here it is in use. It seems silly because it's essentially the same thing. But that's mostly because it's a very simplistic example. But it at least leverages DI and is testable.

app.controller('MyCtrl', function($scope, dialogService) {     $scope.open = function () {         dialogService.open('#modal-to-open');     }; }); 

Anyhow. I hope all of that helps you decide what path you want to take. There are a thousand ways to do this. The "right" way is whatever works, allows you to do whatever you need to do (testing or anything else), and is easy to maintain.

like image 184
Ben Lesh Avatar answered Oct 03 '22 01:10

Ben Lesh