service如下:
define(['js/home/services/services'], function (services) {
'use strict';
services.value('user', function($q,$http){
this.data = {};
this.getData = function(id){
var defered = $q.defer();
var path = "getuser";
return $http.get(path).then(function (d) {
this.data = d;
return $q.when(d);
}, function (d) {
return $q.reject(d);
});
}
});
});
controller如下:
define(['js/home/controllers/controllers'], function (controllers) {
'use strict';
controllers.controller('MyCtrl1', ['$scope','user',function ($scope,user) {
console.log(user);
user.getData("1").then(function (res) {
console.log(res);
//$scope.username = res.username;
}, function (rej) {
// error handler
});
}]);
});
错误信息:
TypeError: user.getData is not a function
at new <anonymous> (http://localhost:8888/erp-web/js/home/controllers/my-ctrl-1.js?version=1447986445598:5:14)
at invoke (http://localhost:8888/erp-web/lib/angular.js?version=1447986445598:4478:17)
at Object.instantiate (http://localhost:8888/erp-web/lib/angular.js?version=1447986445598:4486:27)
at http://localhost:8888/erp-web/lib/angular.js?version=1447986445598:9151:28
at compile (http://localhost:8888/erp-web/lib/angular-ui-router.js?version=1447986445598:4018:28)
at invokeLinkFn (http://localhost:8888/erp-web/lib/angular.js?version=1447986445598:8789:9)
at nodeLinkFn (http://localhost:8888/erp-web/lib/angular.js?version=1447986445598:8289:11)
at compositeLinkFn (http://localhost:8888/erp-web/lib/angular.js?version=1447986445598:7680:13)
at publicLinkFn (http://localhost:8888/erp-web/lib/angular.js?version=1447986445598:7555:30)
at updateView (http://localhost:8888/erp-web/lib/angular-ui-router.js?version=1447986445598:3959:23) <div data-ui-view="" class="ng-scope">
采用AngularRequireSeed的模块化方式,居然不能依赖注入$q和$http,只好从controller吧$q和$http传过去,比较笨得解决方式如下:
define(['js/home/controllers/controllers'], function (controllers) {
});
大家还有没有更好的方式?