Angularjs $资源没有绑定控制器函数中$ scope的数据

时间:2014-03-06 21:40:04

标签: angularjs binding resources scope

//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

绑定新数据

1 个答案:

答案 0 :(得分:0)

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

在ctrl上你的代码是:     $ scope.owner = data [0];

功能编辑器内部:     $ scope.owner = data;

此外,在您评论的函数edituser中     // PARAMS {ID:IDS},

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