我正在研究AngularJS服务,我遇到了问题。
那是我的角色代码:
//img = Uri passed from calling method
File photo = new File(img.getPath());
TypedFile typedFile = new TypedFile("application/octet-stream", photo);
TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient(twitSession);
MediaService mediaService = twitterApiClient.getMediaService();
final StatusesService statusesService = twitterApiClient.getStatusesService();
mediaService.upload(typedFile, null, null, new Callback<Media>() {
@Override
public void success(final Result<Media> result) {
statusesService.update(input, null, null, null, null,
null, null, null, result.data.mediaIdString, new Callback<Tweet>() {
@Override
public void success(Result<Tweet> result) {
Log.d("Twitter image callback", "Successfully posted image and text to Twitter!");
}
@Override
public void failure(TwitterException e) {
if (result != null) {
Log.e("Twitter image callback", "Result is - " + result.toString());
}
Log.d("Twitter image callback", "Failed to post image and text to Twitter!");
}
});
}
@Override
public void failure(TwitterException e) {
Log.d("Twitter image callback", "Failed to upload image to Twitter!");
e.printStackTrace();
}
});
var app = angular.module("todoListApp");
app.controller('MainController', MainController);
MainController.$inject = ['$scope'];
function MainController($scope, dataService){
$scope.helloConsole = dataService.helloConsole;
};
app.service('dataService', function(){
this.helloConsole = function(){
console.log("console services");
};
});
我正在尝试这样做,以便当我点击“保存”时,控制台会向我显示“控制台服务”,但它给了我一个错误:
angular.js:13424 TypeError:无法读取未定义的属性'helloConsole'
答案 0 :(得分:4)
您需要更改编写代码的方式。它应该看起来更像这个
angular.module("todoListApp", [])
.controller('MainController', ['$scope', 'dataService', function($scope, dataService){
$scope.helloConsole = dataService.helloConsole;
}])
.service('dataService', [function(){
this.helloConsole = function(){
console.log("console services");
};
}]);
这也是一项数据服务&#39;是这个带有http调用的gettig数据?因为如果是这样的话就做一个工厂。
答案 1 :(得分:2)
从angular.service
的第二个函数参数返回单个服务对象。此外,如果您明确了解控制器的依赖关系,那么think会更清晰/更好地工作:
var app = angular.module("todoListApp", []);
app.controller('MainController', ['$scope', 'dataService', MainController]);
function MainController($scope, dataService){
$scope.helloConsole = dataService.helloConsole;
$scope.todos = [{txt:"todo 1"}, {txt:"todo 2"}];
}
app.service('dataService', function(){
return {
helloConsole: function(){
console.log("console services");
}
};
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js"></script>
<div ng-app="todoListApp">
<div ng-controller="MainController" class="list">
<div class="item" ng-class="{'editing-item' : editing, 'edited': todo.edited}" ng-repeat="todo in todos">
{{todo.txt}}
<div class="actions">
<a href="" ng-click=" editing = !editing">Edit</a>
<a href="" ng-click="helloConsole()">Save</a>
<a href="" class="delete">Delete</a>
</div>
</div>
</div>
</div>
&#13;
答案 2 :(得分:0)
我重写了一点,所以它更容易理解(至少对我来说)。 我为你的代码添加了一个“todos”数组,只是为了让ng-repeat起火。
var app = angular.module("todoListApp",[])
.service('dataService', function(){
this.helloConsole = function(){
console.log("console services");
};
})
.controller('MainController', [ '$scope', 'dataService',function ($scope,dataService) {
$scope.helloConsole = dataService.helloConsole;
$scope.todos = [ {
"todo":"1"
}
]
}])
;