1

我在这里有一个简单的角度应用程序

<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"

我在 JSFiddle http://jsfiddle.net/charliedavi/bezFB/22/中有它,但它不会运行。真的很奇怪。我认为我的咖啡脚本有错误,但我看不到

错误:

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

在纯 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/

4

2 回答 2

5

我不知道咖啡脚本,但有角度。我只是试图解决它。;-)

  • 选择无包裹体,在选择框架下
  • 选择无库(纯js)
  • 添加 Angular js 作为资源
  • 使用此角度引导程序手动初始化角度

    angular.bootstrap document, ['WhereToMeet']

  • 生成的 javascript 代码在另一个范围内。您必须通过将-b参数添加到咖啡脚本编译器或通过显式导出函数来解决此问题

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

    它现在正在工作Fiddle Here

于 2013-02-23T16:29:14.757 回答
1

昨天我对 jsfiddle 和 angular 有类似的问题。我必须做几件事才能使它工作:

  • Jsfiddle 正在为 html 和 body 添加标签,所以只需编写应该在 body 标签内结束的标记。
  • 添加一个带有 ng-app="myApp" 的包装 div,而不是尝试指定另一个 html-tag
  • 选择无包裹体,在选择框架下

我不知道你的“传单”在做什么,但我已经更新了你的小提琴,这样点击就会触发警报

我必须更改控制器的实例化方式才能使 onclick 正常工作。

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

app.controller("MapCtrl", function ($scope) {
    $scope.clicked = function (clicked) {
        console.log("clicked");
        $scope.shape = "Clicked";
        return alert("clicked");
    };
});
于 2013-02-23T14:50:58.537 回答