【发布时间】:2013-02-09 01:41:39
【问题描述】:
我这里有一个简单的 Angular 应用程序
<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");
};
};
【问题讨论】:
-
1) 您没有选择 CoffeeScript 作为该小提琴中的语言,并且 2) 您的代码有非常不一致的缩进。
-
我更新了缩进,如何启用 JSFiddle 的咖啡脚本
-
添加了一个javascript版本
-
您需要在左侧边栏中选择“no wrap(head)”选项,以便在 HTML 之前插入 JS,以使 Angular 工作。我现在没有收到任何错误,所以问题出在你的逻辑中。
-
在编写函数和指令时最好遵循骆驼符号。