加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何引导数据,因为它是由Angular.js中的$资源服务

发布时间:2020-12-17 09:17:59 所属栏目:安全 来源:网络整理
导读:我有这个Rails应用程序,由UsersController提供一个index.html.erb.在处理该页面的角度控制器中,我为用户提供了$资源服务 CoffeeScript的 .factory('User',['$resource',($resource) - $resource 'api/users/:user_id/:action',{authenticity_token:app.csrf}
我有这个Rails应用程序,由UsersController提供一个index.html.erb.在处理该页面的角度控制器中,我为用户提供了$资源服务

CoffeeScript的

.factory('User',['$resource',($resource) ->
  $resource 'api/users/:user_id/:action',{authenticity_token:app.csrf},query:
      method: 'GET'
      isArray: yes
    new:
      method: 'GET'
      params:
        user_id: 'new'
    update:
      method: 'PUT'
])

控制器提取

window.app = angular.module("app",['userServices'])
.config(["$routeProvider",($routeProvider) ->
  $routeProvider
  .when "/users",templateUrl: "assets/templates/users/index.html"
    controller: UserCtrl

  .otherwise redirectTo: "/users"
])

# users Controllers
UserCtrl = ($scope,User) ->
  User.query (r) ->
    $scope.users = r
    # code..

我认为这是一个很常见的情况,但显然,这个页面只需要一次到服务器.我想知道Angular是否有一种方法来使用一些引导数据,因为它们是从$资源服务中调用的操作返回的数据.

通过将Gon ruby?? gem分配给一个名为Gon的全局变量,我已经对引导数据部分进行了处理.我知道我可以简单地做$scope.users = gon.users.但是,这些用户模型不会得到像$scope.users [0]的优点$save()

谢谢!

事实证明,这真的很简单!

对于实例,假设您拥有变量_tasks和角度$资源模型任务中的所有非角度模型,所有您需要做的是将_tasks逐个传递到构造函数任务中,如下所示:

$scope.tasks = (new Task(task) for task in _tasks)

那么每个$scope.tasks都将具有所有这些$update(),$delete()方法

$scope.tasks[0].$update({due_at: new Date})

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读