2013-08-27 38 views
2

我有一个服务作为角服务返回的字符串作为字典

angular.module('inviteService', ['ngResource']).factory('Invite', function ($resource) { 
    return $resource('/invite'); 
}); 

和我的控制器是

$scope.invite = function() { 
     console.log("submitting invite for " + $scope.email); 

     var invite = new Invite(); 
     Invite.save({'email': $scope.email}, function (data) { 
      console.log('data is: ' + data); 
      $scope.message.type = 'info'; 
      $scope.message.content = data; 
//   console.log('controller message' + JSON.stringify($scope.message, null, 2)); 

      // reset email input box 
      $scope.email = undefined; 
     }); 

和相关指令代码

scope.$watch('ngModel', function() { 
     if (Object.keys(scope.ngModel).length > 0) { 
      console.log('directive message: ' + JSON.stringify(scope.ngModel)); 
      element.show(); 
      //noinspection JSUnresolvedFunction 
      $timeout(function() { 
       //element.empty(); 
       element.fadeOut("slow"); 
      }, 1000); 
     } 
    }, true); 

当我运行代码,我在Chrome Network tab看到的回复为

"You are already on our invite list" 

但角码控制台显示我

data is: [object Object] notificationController.js:13 
directive message:{ 
"type": "info", 
"content": { 
    "0": "\"", 
    "1": "Y", 
    "2": "o", 
    "3": "u", 
    "4": " ", 
    "5": "a", 
    "6": "r", 
    "7": "e", 
    "8": " ", 
    "9": "a", 
    "10": "l", 
    "11": "r", 
    "12": "e", 
    "13": "a", 
    "14": "d", 
    "15": "y", 
    "16": " ", 
    "17": "o", 
    "18": "n", 
    "19": " ", 
    "20": "o", 
    "21": "u", 
    "22": "r", 
    "23": " ", 
    "24": "i", 
    "25": "n", 
    "26": "v", 
    "27": "i", 
    "28": "t", 
    "29": "e", 
    "30": " ", 
    "31": "l", 
    "32": "i", 
    "33": "s", 
    "34": "t", 
    "35": "\"" 
} 

} 这是为什么数据是不是来为字符串?

+0

'console.log(data);'output? – zsong

+0

数据为:{“0”:“\”“,”1“:”Y“,”2“:”o“,”3“:”u“,”4“:”“,”5“ a“,”6“:”r“,”7“:”e“,”8“:”“,”9“:”a“,”10“:”l“,”11“:”r“, “12”:“e”,“13”:“a”,“14”:“d”,“15”:“y”,“16”:“”,“17”:“o” :“n”,“19”:“”,“20”:“o”,“21”:“u”,“22”:“r”,“23”:“”,“24” “25”:“n”,“26”:“v”,“27”:“i”,“28”:“t”,“29”:“e”,“30”:“”,“31 “:”l“,”32“:”i“,”33“:”s“,”34“:”t“,”35“:”\“”} – daydreamer

+0

您可以使用文本/像这样'$ httpProvider.defaults.headers.common ['Accept'] ='text/plain';'? – zsong

回答

1

ngResource需要来自服务器的响应中的对象或对象数组。

为你的选择见https://stackoverflow.com/a/13816008/215945

  1. 使用$ http来代替$资源
  2. 回报从你的服务器的对象(可能是最简单的方法,如果你可以修改服务器):
    { "str": "'You are...'"}
  3. 拦截并修改返回值(可能工作太多)
+0

我做了2.)因为我是写服务器的人以及:)。谢谢@Mark – daydreamer

相关问题