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) {
'use strict';
controllers.controller('MyCtrl1', ['$q','$http','$scope','user',function ($q,$http,$scope,user) {
var u = new user($q,$http);
u.getData("1").then(function (res) {
$scope.username = res.data.userName;
}, function (rej) {
});
}]);
});
大家还有没有更好的方式?
用factory试试
angular.module('starter.ForgetService',[])
.factory('ForgetService',['$rootScope','$timeout','$ionicLoading','$ionicPopup',function($rootScope,$timeout,$ionicLoading,$ionicPopup) {
return{
sendData:function(message)
{
console.log('{"tell":"'+message.phone+'","id":"'+message.id+'"}');
console.log(window.localStorage.wsurl);
$.ajax({
url: window.localStorage.wsurl +"/ECourierServe/servlet/ReSetPassWord",
type: "POST",
contentType: "application/x-www-form-urlencoded",
data: 'json='+'{"tell":"'+message.phone+'","id":"'+message.id+'"}',
dataType: "json",
success: function (result) {
console.log(result);
$ionicLoading.hide();
$ionicPopup.alert({
title: '提示',
template:result.returnMessage
});
},
error: function (xhr, ajaxOptions, thrownError) {
console.log("错误信息 " + thrownError, 'error');
$ionicLoading.hide();
$ionicPopup.alert({
title: '提示',
template:'联网异常'
});
}
});
}
}
}]);