【问题标题】:Angularjs $resource not binding data with $scope in controller functionAngularjs $resource 未在控制器函数中将数据与 $scope 绑定
【发布时间】:2014-04-09 19:18:49
【问题描述】:
//my controller using $resource factory variable ownerSrvc
Owner.factory('OwnerSrvc', function($resource){
    return $resource('/owner/:id', {

            query: { method: 'GET', isArray: true },
            create: { method: 'POST' },
            show: { method: 'GET', isArray:true},
            update: { method: 'PUT', params: {id: '@id'} },
            delete: { method: 'DELETE', params: {id: '@id'} }
        }
    )
});

Owner.controller('ownerCtrl', function($scope, OwnerSrvc, $location){
    OwnerSrvc.query({id:'531784367883254406700e99'},function(data) {
        //  console.log(data[0]);        retrievig data of Mr. Ali
        $scope.owner = data[0];        binding works here at controller level;
    });

    //User defined function at Congtroller level

    $scope.editUser=function(ids){  //when i called this function later with id
        OwnerSrvc.query( //params{id:ids},    //my service giving me result fine
            function(data) {
                console.log(data[0]);                 //result showing in console OK  i.e. Mr. Zain
                $scope.owner = data;   // but data not binding with $scope.owner

            });

        console.log($scope.owner)  // here after Query called, $scope.owner showing old result i.e Mr. Ali instead of Mr. Zain
        $location.path('/owner');
     };
});

我的问题是为什么数据没有在控制器内部的功能级别绑定。以及如何将新数据与$scope.owner绑定?

【问题讨论】:

  • 请更正格式。

标签: angularjs binding resources scope


【解决方案1】:

您似乎没有在函数内部分配相同的属性。

在 ctrl 上,您的代码是: $scope.owner = 数据[0];

在函数 edituser 内部: $scope.owner = 数据;

此外,在您评论的函数 edituser 内 //参数{id:ids},

如果你不想使用 param,你至少应该写 {},

【讨论】:

    猜你喜欢
    • 2018-09-29
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多