Angular app wont load (JSFiddle)

I have a simple angular app here

<div ng-app="WhereToMeet" ng-controller="MapCtrl">
  <leaflet shape="shape"></leaflet>
  <button ng-click="clicked()">Clicked</button>
</div>


app = angular.module("WhereToMeet", [])

app.directive "leaflet",  ->
  restrict: "E"
  replace: true
  transclude: true
  template: "<div id=\"map\"></div>"
  scope:
    shape: "=shape"
  link: (scope, element, attrs, ctrl) ->
    scope.$watch attrs.shape,( (newValue, oldValue) ->
      watched newValue
    ), true

  watched = (newValue) ->
    alert newValue


@MapCtrl = ($scope)  ->
  clicked = (clicked) ->
    $scope.shape = "Clicked"
    alert "clicked"

I have it in a JSFiddle http://jsfiddle.net/charliedavi/bezFB/22/ but it wont run. Really odd. I think its an error with my coffee script but I can not see it

error:

Uncaught SyntaxError: Unexpected string fiddle.jshell.net:22
Uncaught Error: No module: WhereToMeet 

in pure JS

var app;

app = angular.module("WhereToMeet", []);

app.directive("leaflet", function() {



      var watched;
      ({
        restrict: "E",
        replace: true,
        transclude: true,
        template: "<div id=\"map\"></div>",
        scope: {
          shape: "=shape"
        },
        link: function(scope, element, attrs, ctrl) {
          return scope.$watch(attrs.shape, (function(newValue, oldValue) {
            return watched(newValue);
          }), true);
        }
      });
      return watched = function(newValue) {
        return alert(newValue);
      };
    });

    this.MapCtrl = function($scope) {
      var clicked;
      return clicked = function(clicked) {
        $scope.shape = "Clicked";
        return alert("clicked");
      };
    };

http://jsfiddle.net/charliedavi/gsPx3/2/

i dont know coffee script but angular. i just tried to solve it. ;-)

  • Select no-wrap body, under select framework
  • Select no-library(pure-js)
  • Add angular js as resources
  • Manually initialize angular using this angular bootstrap

    angular.bootstrap document, ['WhereToMeet']

  • The generated javascript code is in another scope. You have to solve this by either adding the -b parameter to the coffeescript compiler or export your function explicitly via

    root = exports ? this
    root.clicked = ->
          alert "clicked"
          $scope.shape = "Clicked"
    

    It is working now Fiddle Here

I had a similar issue with jsfiddle and angular yesterday. I had to do a couple of things to make it work:

  • Jsfiddle is adding the tags for html and body, so just write the markup that should end up inside the body tag.
  • Add a wrapping div with ng-app="myApp" instead of trying to specify another html-tag
  • Select no-wrap body, under select framework

I don't know what your "leaflet" is doing but I have updated your fiddle so that the click will trigger an alert

I've had to change the how the controller is instantiated to get the onclick to work.

http://jsfiddle.net/t9nsY/2/

app.controller("MapCtrl", function ($scope) {
    $scope.clicked = function (clicked) {
        console.log("clicked");
        $scope.shape = "Clicked";
        return alert("clicked");
    };
});