3

I'm trying to get a jquery-ui menu to work inside an AngularJS directive.

I have tried this fiddle with an example of what I'm trying to do.

html:

<div ng-app="myApp" ng-controller="mainController">
  <ul j-menu>
    <li>Test 1</li>
    <li>Test 2</li>
  </ul>
</div>


js:

var myapp = angular.module("myApp", []);

myapp.directive("jMenu", function() {
  return {
    restrict: "A",
    link: function(scope, element, attrs) {
      $(element).menu();
    }
  }
});

myapp.controller("mainController", ['$scope', function($scope) {}]);

The most perplexing piece is that I have modified another 'working' fiddle to do exactly what I want to do but cannot replicate in my own fiddle nor in my own dev env.

What am I missing?!

1
  • missing scope: {} ? Commented Dec 11, 2015 at 5:51

1 Answer 1

4

It seems that there's a problem with how jsfiddle is handling external resource with AngularJS, it keeps logging out $injector errors in the console. I've copied and pasted the external resources in a plunker and everything else in your first fiddle and it works just as how its supposed to be.

DEMO

Note: Since you've added jquery, all angular elements are also jquery elements. So by treating elements as such you can do this:

instead of

$(element).menu()

you can do this

element.menu()

Update:

As what Ali pointed out, JSFiddle has the option to change the Load Type to No wrap - in <body in the javascript settings located at the javascript textarea's top-right corner.

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

Very interesting find. After looking at my console window's output on my dev setup, I saw some errors that I thought were 'normal'. After correcting them, my local solution also worked. Thanks!
There is no issue with jsfiddle. Just change the load type in the javascript area to No wrap - in <body> and it will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.