简体   繁体   English

如何在AngularJS中访问Parse对象属性?

[英]How to access Parse object properties in AngularJS?

I'm building out an AngularJS app that uses data from a parse-server backend. 我正在构建一个使用来自解析服务器后端的数据的AngularJS应用程序。 I retrieve any data through services, which can be called from my controllers. 我通过服务检索任何数据,可以从我的控制器调用。 But due to how Parse objects work all properties are accessed through a get function, and my html becomes filled with lines like <p>{{myObject.get('title')}</p>} . 但是由于Parse对象的工作方式,所有属性都是通过get函数访问的,而我的html中充满了像<p>{{myObject.get('title')}</p>}

Ideally I would like to access properties just like a regular object eg myObject.title , but I can't seem to find a reference for best practices when using the Parse JS sdk with AngularJS. 理想情况下,我myObject.title常规对象一样访问属性,例如myObject.title ,但在使用带有AngularJS的Parse JS sdk时,我似乎无法找到最佳实践的参考。

My searches have turned up several people pointing to this example of Parse and AngularJS boilerplate by BRANDiD but links to their actual code and site seem to be dead. 我的搜索已经发现有几个人指着BRANDiD的Parse和AngularJS样板的例子,但链接到他们的实际代码和站点似乎已经死了。

Any insight into how this is best approached would be much appreciated! 任何有关如何最好地接近这一点的见解将非常感谢!

I would create a service that would return a JSON converted parse response. 我会创建一个服务,返回一个JSON转换的解析响应。

(Pseudo) code: (伪)代码:

Service: 服务:

app.service('Books', function($q) {
  return {
    findBookById: function(id) {

      var deferred = $q.defer();

      // parse query logics .. 
      var Book = Parse.Object.extend("Book");
      var query = new Parse.Query(Book);
      query.equalTo("book_id", id);

      query.first({
        success: function(response) {
          // convert to JSON so that you can access properties directly
          // eg. book.name (instead of book.get('name'))
          deferred.resolve(response.toJSON());
        },
        error: function(err) {
          deferred.reject(err);
        }
      });
      return deferred.promise;
    }
  };
});

Controller: 控制器:

app.controller('BookCtrl', function($scope, Books) {
  Books
    .findById(10)
    .then(function(book) {
      $scope.book = book;
    })
    .catch(function(err) {
      alert('Error occurred : ' + err.message);
    });
});

View: 视图:

<p>Book Name: {{book.name}}</p>

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

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