2017-04-08 107 views
1

我目前正试图通过使用Twitter API直接从我的网站发布推文,但我是Angular的新手,并且有一些关于在url中传递参数的问题。 我想将状态变量传递给URL,因为我将从textarea获取输入。

service.js

angular.module('twitterApp.services', []) 
.factory('twitterService', function($q, $scope){ 
    var authorizationResult = false; 

    return { 
     initialize: function() { 
      OAuth.initialize('#OAuth Key here', {cache: true}); 
      authorizationResult = OAuth.create('twitter'); 
     }, 
     isReady: function() { 
      return(authorizationResult); 
     }, 
     connectTwitter: function() { 
      var deferred = $q.defer(); 
      OAuth.popup('twitter', {cache: true}, function(error, result){ 
       if(!error){ 
        authorizationResult = result; 
       } 
       else{ 
        //Do Something else if there's an error 
       } 
       return deferred.promise; 
      }); 
     }, 
     clearCache: function() { 
      OAuth.clearCache('twitter'); 
      authorizationResult = false; 
     }, 
     postTweet() { 
      var status = $scope.textModel; 
      var deferred = $q.defer(); 
      var promise = authorizationResult.post('/1.1/statuses/update.json?status=' + 'status').done(function(data){ 
       deferred.resolve(data); 
      }); 
      return deferred.promise; 
     } 
    } 
}); 

这里是我的控制,我声明的变量,我敢肯定,我错了,但我需要知道这个解决方案。 控制器

app.controller('TwitterController', function($scope, $q, twitterService) { 

    $scope.textModel = ""; 

    twitterService.initialize(); 

    $scope.postTweet = function() { 
     var status = $scope.textModel; 
    } 
}); 

的index.html

<!DOCTYPE html> 
<html ng-app="twitterApp"> 
<head> 
    <title>Twitter OAuth.io Example</title> 
    <link rel="stylesheet" href="custom.css"> 
    <link rel="stylesheet" href="bootstrap.min.css"> 
    <script src="jquery-3.1.1.js"></script> 
    <script src="bootstrap.min.js"></script> 
    <script src="oauth.js"></script> 
    <script src="angular.min.js"></script> 
    <script src="app.js"></script> 
    <script src="controller.js"></script> 
    <script src="services.js"></script> 
</head> 
<body> 
    <div class="container text-center" ng-controller="TwitterController"> 
     <h1>Tweet..</h1> 
     <span><i>Or rather just do it more than classic version.</i></span> 
     <div class="row"> 
      <div> 
       <textarea placeholder="What's on your mind?" id="tweetBox" rows="10" ng-model="textModel"></textarea> 
      </div> 
     </div> 
     <div> 
      <button class="btn btn-lg" id="tweetButton" ng-click="postTweet()">Tweet</button> 
     </div> 
     <div> 
      Your tweet will appear like this: 
     </div> 
     <div> 
      " {{textModel}} " 
     </div> 
    </div> 
</body> 
</html> 
+0

你面临的问题是什么? – Sarkhan

+0

@Sarkhan我想在textarea中写一些东西,并将它作为状态提交给twitter。问题是我必须在url中传递它,为了做到这一点,所以我将变量设置为'status',可以将textarea的值保存为$ scope.textModel。但我不知道如何在url中传递它,因为url在服务中,而我的变量在控制器中。 –

回答

3

您可以将参数传递到您的postTweet功能。你的服务的

部分:

postTweet(text) { 
     var status = text; 
     var deferred = $q.defer(); 
     var promise = authorizationResult.post('/1.1/statuses/update.json?status=' + 'status').done(function(data){ 
      deferred.resolve(data); 
     }); 
     return deferred.promise; 
    } 

而且你的控制器:

app.controller('TwitterController', function($scope, $q, twitterService) { 

    $scope.textModel = ""; 

    twitterService.initialize(); 

    $scope.postTweet = function() { 
     twitterService.postTweet($scope.textModel); 
    } 
}); 
+0

我完全了解这个概念,但我仍然遇到了错误。我不知道为什么。 –

+0

告诉我你的错误,也许我可以帮你。 –

+0

嘿!我现在真的需要帮助我的代码。你能帮忙吗? –

2

本质上面的答案是正确的,我只想补充一点,处理错误的服务是很重要的或至少在控制器中,现在只有成功调用deferred.resolve()。您应该捕获任何错误的Twitter做一个deferred.reject(ERR) 所以你的控制器看起来像:

$scope.postTweet = function() { 
     twitterService.postTweet($scope.textModel) 
     .then(data => console.log(data)) 
     .catch(err => console.error(err)); 
    } 

我敢肯定有更好的方法来处理成功/错误比CONSOLE.LOG但你的我希望的想法,不要忘记错误的情况。

+0

当然,但我得到一个错误。你能帮忙吗? –

+0

当然可以。告诉我更多关于你的错误的信息吗? –

相关问题