Ruby on rails 如何使用角度/轨道创建非全局名称间隔控制器?
根据注释编辑代码 app.js.coffeeRuby on rails 如何使用角度/轨道创建非全局名称间隔控制器?,ruby-on-rails,angularjs,Ruby On Rails,Angularjs,根据注释编辑代码 app.js.coffee controllers = angular.module('Lunch.controllers', [] ) services = angular.module('Lunch.services', [] ) app = angular.module('Lunch', ['ngRoute','Lunch.controllers', 'Lunch.services']) app.config([ '$routeProvider', ($routePr
controllers = angular.module('Lunch.controllers', [] )
services = angular.module('Lunch.services', [] )
app = angular.module('Lunch', ['ngRoute','Lunch.controllers', 'Lunch.services'])
app.config([ '$routeProvider', ($routeProvider) ->
$routeProvider.when("/", { templateUrl: "/ngviews/lunch.html", controller: "LunchPoolController" } )
])
controllers.controller('LunchPoolController', ($scope, LunchMates) ->
# $scope.lunchers = ["some data"]
LunchMates.getLunchMates().then (data) ->
$scope.lunchers = data
)
services.factory 'LunchMates', ($rootScope, $q, $http) ->
LunchMates =
getLunchMates: () ->
d = $q.defer();
$http.get('/lunchers').then (response, status) ->
d.resolve(response.data)
return d.promise
return LunchMates
咖啡
controllers = angular.module('Lunch.controllers', [] )
services = angular.module('Lunch.services', [] )
app = angular.module('Lunch', ['ngRoute','Lunch.controllers', 'Lunch.services'])
app.config([ '$routeProvider', ($routeProvider) ->
$routeProvider.when("/", { templateUrl: "/ngviews/lunch.html", controller: "LunchPoolController" } )
])
controllers.controller('LunchPoolController', ($scope, LunchMates) ->
# $scope.lunchers = ["some data"]
LunchMates.getLunchMates().then (data) ->
$scope.lunchers = data
)
services.factory 'LunchMates', ($rootScope, $q, $http) ->
LunchMates =
getLunchMates: () ->
d = $q.defer();
$http.get('/lunchers').then (response, status) ->
d.resolve(response.data)
return d.promise
return LunchMates
咖啡服务
controllers = angular.module('Lunch.controllers', [] )
services = angular.module('Lunch.services', [] )
app = angular.module('Lunch', ['ngRoute','Lunch.controllers', 'Lunch.services'])
app.config([ '$routeProvider', ($routeProvider) ->
$routeProvider.when("/", { templateUrl: "/ngviews/lunch.html", controller: "LunchPoolController" } )
])
controllers.controller('LunchPoolController', ($scope, LunchMates) ->
# $scope.lunchers = ["some data"]
LunchMates.getLunchMates().then (data) ->
$scope.lunchers = data
)
services.factory 'LunchMates', ($rootScope, $q, $http) ->
LunchMates =
getLunchMates: () ->
d = $q.defer();
$http.get('/lunchers').then (response, status) ->
d.resolve(response.data)
return d.promise
return LunchMates
铬合金控制台发出咳嗽声
Uncaught ReferenceError: controllers is not defined controllers.js?body=1:2
Uncaught ReferenceError: services is not defined services.js?body=1:2
Error: [$injector:unpr] Unknown provider: LunchMatesProvider <- LunchMates
未捕获引用错误:未定义控制器控制器。js?body=1:2
未捕获引用错误:未定义服务。js?body=1:2
错误:[$injector:unpr]未知提供程序:午餐伴侣提供程序您应该按照模块依赖的顺序定义模块
因此,首先加载您的
services.js
var services = angular.module('Lunch.services',[])
services.factory('MyLunchFactory',function($http){ ... });
然后是controllers.js
var controllers = angular.module('Lunch.controllers',[]);
controllers.controller('LunchPoolController',function($scope){ ... });
最后是你的app.js(使用你的ng应用程序上的名称)
查看ng,book的第36页,我应该能够使用类似var app=angular.module('app',[])的东西;app.controller('FirstController',function($scope){$scope.message=“hello”;});但这在本例中不起作用,我刚刚编辑了上面的内容来修复午餐=angular.module line噢,对不起,似乎您加载文件的顺序不对。我将更新我的答案。我不确定这是否对我有效,我发布了我的编辑,我是否遗漏了什么?出于好奇,这些文件的加载顺序是什么?您是如何知道的?您很可能使用了ng应用程序指令。在模块声明中,angular将检查此指令并自动引导应用程序。在此引导发生后,您不能再添加控制器/模块等。因此,您必须确保您的ng应用程序模块(“午餐”)最后加载。然后,它可以找到您的依赖模块,并自动引导