简体   繁体   English

AngularJS + Restangular-调用.one()和.get()结果数组吗?

[英]AngularJS + Restangular - Calling to .one() and .get() results in array?

I am using Angular 1.0.8 along with Restangular for this. 我为此使用Angular 1.0.8和Restangular。 Since I am just starting out in js framework in general, a newbie-friendly explanation would be greatly appreciated. 由于我一般只是从js框架开始,因此非常感谢新手友好的解释。

I have a very simple HTML <section> like this (ng-app has been defined in body ): 我有一个非常简单的HTML <section>像这样(ng-app已在body定义):

<section class="comment-list block scrollable wrapper" style="height:350px" ng-controller="MessageStreamController">
    <div> {{ message.message }} </div>
    <div class="input-group">
        <input type="text" class="form-control" ng-change="change()" ng-model="message.message" placeholder="Input your comment here">
          <span class="input-group-btn">
            <button class="btn btn-primary" type="button" ng-click="clicked()">POST</button>
          </span>
    </div>
</section>

with the following <script> 使用以下<script>

var JApp = angular.module('JApp', ['restangular']);

JApp.controller('MessageStreamController', function($scope, Restangular) {
  var service = Restangular.all('message-stream');

  $scope.clicked = function() {
    $scope.message = service.one('index', 1).get();
  }
})

In the backend /message-stream/index/1 it simply returns 在后端/message-stream/index/1它只是返回

return json_encode(array(
    'message' => 'Hey there James',
    'user' => 'Terry'
));

This results in the <div> {{ message.message }} </div> and input gets rendered with 'Hey there James'. 这将导致<div> {{ message.message }} </div>并且input将显示为“ Hey there James”。 All good so far. 到目前为止一切都很好。

But then I can't edit the input box afterwards. 但是之后我无法编辑输入框。

Some questions pointed out that this happens when you don't use ng-repeat, but then again I am expecting one JSON object from the backend. 一些问题指出,当您不使用ng-repeat时会发生这种情况,但是我又期望后端有一个JSON对象。

Any pointer here to help? 这里有任何帮助的指针吗?

UPDATE: Changed from Restangular to $resource fixes my problem. 更新:从Restangular更改为$ resource解决了我的问题。 Though would love to know why this is happening. 虽然很想知道为什么会这样。 Answer from people who has experience with Restangular will be appreciated. 来自具有Restangular经验的人的答案将不胜感激。

Restangular returns a promise from all queries which can not be edited. Restangular从所有无法返回的查询中返回一个承诺。

Instead you should assign the actual result to your scope as per the answer to this question - Angular.JS: why can't the inputs be edited? 相反,您应该根据此问题的答案将实际结果分配给您的范围-Angular.JS:为什么不能编辑输入?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM