Javascript 如何使用CoffeeScript正确定义服务、工厂、目录和控制器
我对AngularJS还是新手,我只是想知道如何使用Javascript 如何使用CoffeeScript正确定义服务、工厂、目录和控制器,javascript,angularjs,coffeescript,Javascript,Angularjs,Coffeescript,我对AngularJS还是新手,我只是想知道如何使用CoffeeScript语法正确定义服务,工厂,目录和控制器 一些带有描述和解释“为什么会这样”的例子将是完美的。一个使用控制器和咖啡脚本的TodoList的好例子: angular.module('TodoApp').controller 'TodoCtrl', ($scope) -> $scope.todos = [ {text: 'learn angular', done: true}, {text: 'bui
CoffeeScript
语法正确定义服务
,工厂
,目录
和控制器
一些带有描述和解释“为什么会这样”的例子将是完美的。一个使用
控制器和咖啡脚本的TodoList的好例子:
angular.module('TodoApp').controller 'TodoCtrl', ($scope) ->
$scope.todos = [
{text: 'learn angular', done: true},
{text: 'build an angular app', done: false}
]
$scope.addTodo = ->
$scope.todos.push({text: $scope.todoText, done: false})
$scope.todoText = ''
$scope.remaining = ->
count = 0
for todo in $scope.todos
count += todo.done ? 0: 1
count
$scope.archive = ->
oldTodos = $scope.todos
$scope.todos = []
for todo in oldTodos
$scope.todos.push(todo) unless todo.done
使用controller
和CoffeeScript
的TodoList的一个很好的示例:
angular.module('TodoApp').controller 'TodoCtrl', ($scope) ->
$scope.todos = [
{text: 'learn angular', done: true},
{text: 'build an angular app', done: false}
]
$scope.addTodo = ->
$scope.todos.push({text: $scope.todoText, done: false})
$scope.todoText = ''
$scope.remaining = ->
count = 0
for todo in $scope.todos
count += todo.done ? 0: 1
count
$scope.archive = ->
oldTodos = $scope.todos
$scope.todos = []
for todo in oldTodos
$scope.todos.push(todo) unless todo.done
它与在CoffeeScript
中编写普通javascript函数没有任何区别。您可以转换js代码
js和CoffeeScript
JavaScript
var app = angular.module('myApp');
app.directive('dTable', [
'$compile', function ($compile) {
'use strict';
return {
restrict: 'E',
replace: true,
template: '<table class=\"table table-striped table-bordered dataTable\"></table>',
link: function (scope, element, attrs) {
var dataTable,
options = {
"bStateSave": true,
"iCookieDuration": 2419200,
"bJQueryUI": false,
"bPaginate": true,
"bLengthChange": false,
"bFilter": true,
"bInfo": true,
"bDestroy": true,
"iDisplayLength": 10,
"sDom": "lftip",
"sPaginationType": "bootstrap",
"oLanguage": {
"sLengthMenu": "_MENU_ records per page"
}
},
opts = {};
element = $(element);
if (attrs.aaOptions) {
angular.extend(options, scope.$eval(attrs.aaOptions));
}
dataTable = element.dataTable(options);
}
};
}
]);
app = angular.module("myApp")
app.directive "dTable", ["$compile", ($compile) ->
"use strict"
restrict: "E"
replace: true
template: "<table class=\"table table-striped table-bordered dataTable\"></table>"
link: (scope, element, attrs) ->
dataTable = undefined
options =
bStateSave: true
iCookieDuration: 2419200
bJQueryUI: false
bPaginate: true
bLengthChange: false
bFilter: true
bInfo: true
bDestroy: true
iDisplayLength: 10
sDom: "lftip"
sPaginationType: "bootstrap"
oLanguage:
sLengthMenu: "_MENU_ records per page"
opts = {}
element = $(element)
angular.extend options, scope.$eval(attrs.aaOptions) if attrs.aaOptions
dataTable = element.dataTable(options)
]
var-app=angular.module('myApp');
应用指令('dTable'[
“$compile”,函数($compile){
"严格使用",;
返回{
限制:'E',
替换:正确,
模板:“”,
链接:函数(范围、元素、属性){
var数据表,
选项={
“bStateSave”:正确,
“iCookieDuration”:2419200,
“bJQueryUI”:错,
“bPaginate”:对,
“bLengthChange”:false,
“bFilter”:没错,
“宾福”:没错,
是的,
“iDisplayLength”:10,
“sDom”:“lftip”,
“sPaginationType”:“引导程序”,
“语言”:{
“sLengthMenu”:“\u MENU\u每页记录”
}
},
opts={};
元素=$(元素);
如果(属性aaOptions){
角度扩展(选项,范围$eval(属性aaOptions));
}
dataTable=元素。dataTable(选项);
}
};
}
]);
咖啡脚本
var app = angular.module('myApp');
app.directive('dTable', [
'$compile', function ($compile) {
'use strict';
return {
restrict: 'E',
replace: true,
template: '<table class=\"table table-striped table-bordered dataTable\"></table>',
link: function (scope, element, attrs) {
var dataTable,
options = {
"bStateSave": true,
"iCookieDuration": 2419200,
"bJQueryUI": false,
"bPaginate": true,
"bLengthChange": false,
"bFilter": true,
"bInfo": true,
"bDestroy": true,
"iDisplayLength": 10,
"sDom": "lftip",
"sPaginationType": "bootstrap",
"oLanguage": {
"sLengthMenu": "_MENU_ records per page"
}
},
opts = {};
element = $(element);
if (attrs.aaOptions) {
angular.extend(options, scope.$eval(attrs.aaOptions));
}
dataTable = element.dataTable(options);
}
};
}
]);
app = angular.module("myApp")
app.directive "dTable", ["$compile", ($compile) ->
"use strict"
restrict: "E"
replace: true
template: "<table class=\"table table-striped table-bordered dataTable\"></table>"
link: (scope, element, attrs) ->
dataTable = undefined
options =
bStateSave: true
iCookieDuration: 2419200
bJQueryUI: false
bPaginate: true
bLengthChange: false
bFilter: true
bInfo: true
bDestroy: true
iDisplayLength: 10
sDom: "lftip"
sPaginationType: "bootstrap"
oLanguage:
sLengthMenu: "_MENU_ records per page"
opts = {}
element = $(element)
angular.extend options, scope.$eval(attrs.aaOptions) if attrs.aaOptions
dataTable = element.dataTable(options)
]
app=angular.module(“myApp”)
app.directive“dTable”[“$compile”,($compile)->
“严格使用”
限制:“E”
替换:正确
模板:“”
链接:(范围、元素、属性)->
数据表=未定义
选择权=
bStateSave:正确
ICookie持续时间:2419200
bJQueryUI:错
B:对
bLengthChange:false
B过滤器:正确
宾福:是的
B:是的
i显示长度:10
sDom:“lftip”
sPaginationType:“引导”
语言:
SLENNGMENU:“\u菜单\u每页记录”
opts={}
元素=$(元素)
angular.extend选项,scope.$eval(attrs.aaOptions)如果attrs.aaOptions
dataTable=元素。dataTable(选项)
]
它应该与在CoffeeScript
中编写普通javascript函数没有任何区别。您可以转换js代码
js和CoffeeScript
JavaScript
var app = angular.module('myApp');
app.directive('dTable', [
'$compile', function ($compile) {
'use strict';
return {
restrict: 'E',
replace: true,
template: '<table class=\"table table-striped table-bordered dataTable\"></table>',
link: function (scope, element, attrs) {
var dataTable,
options = {
"bStateSave": true,
"iCookieDuration": 2419200,
"bJQueryUI": false,
"bPaginate": true,
"bLengthChange": false,
"bFilter": true,
"bInfo": true,
"bDestroy": true,
"iDisplayLength": 10,
"sDom": "lftip",
"sPaginationType": "bootstrap",
"oLanguage": {
"sLengthMenu": "_MENU_ records per page"
}
},
opts = {};
element = $(element);
if (attrs.aaOptions) {
angular.extend(options, scope.$eval(attrs.aaOptions));
}
dataTable = element.dataTable(options);
}
};
}
]);
app = angular.module("myApp")
app.directive "dTable", ["$compile", ($compile) ->
"use strict"
restrict: "E"
replace: true
template: "<table class=\"table table-striped table-bordered dataTable\"></table>"
link: (scope, element, attrs) ->
dataTable = undefined
options =
bStateSave: true
iCookieDuration: 2419200
bJQueryUI: false
bPaginate: true
bLengthChange: false
bFilter: true
bInfo: true
bDestroy: true
iDisplayLength: 10
sDom: "lftip"
sPaginationType: "bootstrap"
oLanguage:
sLengthMenu: "_MENU_ records per page"
opts = {}
element = $(element)
angular.extend options, scope.$eval(attrs.aaOptions) if attrs.aaOptions
dataTable = element.dataTable(options)
]
var-app=angular.module('myApp');
应用指令('dTable'[
“$compile”,函数($compile){
"严格使用",;
返回{
限制:'E',
替换:正确,
模板:“”,
链接:函数(范围、元素、属性){
var数据表,
选项={
“bStateSave”:正确,
“iCookieDuration”:2419200,
“bJQueryUI”:错,
“bPaginate”:对,
“bLengthChange”:false,
“bFilter”:没错,
“宾福”:没错,
是的,
“iDisplayLength”:10,
“sDom”:“lftip”,
“sPaginationType”:“引导程序”,
“语言”:{
“sLengthMenu”:“\u MENU\u每页记录”
}
},
opts={};
元素=$(元素);
如果(属性aaOptions){
角度扩展(选项,范围$eval(属性aaOptions));
}
dataTable=元素。dataTable(选项);
}
};
}
]);
咖啡脚本
var app = angular.module('myApp');
app.directive('dTable', [
'$compile', function ($compile) {
'use strict';
return {
restrict: 'E',
replace: true,
template: '<table class=\"table table-striped table-bordered dataTable\"></table>',
link: function (scope, element, attrs) {
var dataTable,
options = {
"bStateSave": true,
"iCookieDuration": 2419200,
"bJQueryUI": false,
"bPaginate": true,
"bLengthChange": false,
"bFilter": true,
"bInfo": true,
"bDestroy": true,
"iDisplayLength": 10,
"sDom": "lftip",
"sPaginationType": "bootstrap",
"oLanguage": {
"sLengthMenu": "_MENU_ records per page"
}
},
opts = {};
element = $(element);
if (attrs.aaOptions) {
angular.extend(options, scope.$eval(attrs.aaOptions));
}
dataTable = element.dataTable(options);
}
};
}
]);
app = angular.module("myApp")
app.directive "dTable", ["$compile", ($compile) ->
"use strict"
restrict: "E"
replace: true
template: "<table class=\"table table-striped table-bordered dataTable\"></table>"
link: (scope, element, attrs) ->
dataTable = undefined
options =
bStateSave: true
iCookieDuration: 2419200
bJQueryUI: false
bPaginate: true
bLengthChange: false
bFilter: true
bInfo: true
bDestroy: true
iDisplayLength: 10
sDom: "lftip"
sPaginationType: "bootstrap"
oLanguage:
sLengthMenu: "_MENU_ records per page"
opts = {}
element = $(element)
angular.extend options, scope.$eval(attrs.aaOptions) if attrs.aaOptions
dataTable = element.dataTable(options)
]
app=angular.module(“myApp”)
app.directive“dTable”[“$compile”,($compile)->
“严格使用”
限制:“E”
替换:正确
模板:“”
链接:(范围、元素、属性)->
数据表=未定义
选择权=
bStateSave:正确
ICookie持续时间:2419200
bJQueryUI:错
B:对
bLengthChange:false
B过滤器:正确
宾福:是的
B:是的
i显示长度:10
sDom:“lftip”
sPaginationType:“引导”
语言:
SLENNGMENU:“\u菜单\u每页记录”
opts={}
元素=$(元素)
angular.extend选项,scope.$eval(attrs.aaOptions)如果attrs.aaOptions
dataTable=元素。dataTable(选项)
]
为什么要用减号表示问题?请看一看,其中有咖啡脚本的支持和示例。其中一个例子是肯定的,在ng样板文件
repo网站上有一个例子,但只有一个(针对工厂)。此项目还支持CoffeeScript文件-agree。我对stackoverflow也是个新手,我认为这个地方适合回答那些简单的问题。你给了我一个与我无关的东西负号——你应该说“谷歌”,这将是我问题的“答案”和解决方案。没用。和平。只是一个很好的模板和支持。我知道这只是一个工厂,但定义服务和控制器的方式是一样的。下次,像你说的那样用谷歌搜索。也许这也不是一个简单问题的地方。为什么减号表示问题?看一看