Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $scope不';t外部回调函数_Javascript_Angularjs_Pouchdb - Fatal编程技术网

Javascript $scope不';t外部回调函数

Javascript $scope不';t外部回调函数,javascript,angularjs,pouchdb,Javascript,Angularjs,Pouchdb,我开发了与数据库集成的AngularJS应用程序。当尝试从数据库获取信息时,$scope变量仅存在于方法内部 db.allDocs({include_docs: true, descending: true}, function(err, doc) { $scope.$apply(function(){ $scope.info = doc.rows; }); }); $scope.select = function(id){ for(var

我开发了与数据库集成的AngularJS应用程序。当尝试从数据库获取信息时,$scope变量仅存在于方法内部

db.allDocs({include_docs: true, descending: true}, function(err, doc) {
    $scope.$apply(function(){  
        $scope.info = doc.rows;
    });
});

$scope.select = function(id){

        for(var i = 0; i < $scope.info.length; i++){
            if(id == $scope.info[i].doc._id){
                $scope.$apply(function (){
                    $scope.sName = $scope.info[i].doc.name;
                    $scope.sSurname = $scope.info[i].doc.surname;
                    $scope.sPhone = $scope.info[i].doc.phone;
                    $scope.sAddress = $scope.info[i].doc.address;
                    console.log($scope.info[i].doc);
                });


            }
        }

    };
db.allDocs({include_docs:true,descending:true},函数(err,doc){
$scope.$apply(函数(){
$scope.info=doc.rows;
});
});
$scope.select=函数(id){
对于(变量i=0;i<$scope.info.length;i++){
如果(id=$scope.info[i].doc.\u id){
$scope.$apply(函数(){
$scope.sName=$scope.info[i].doc.name;
$scope.sSurname=$scope.info[i].doc.name;
$scope.sPhone=$scope.info[i].doc.phone;
$scope.sAddress=$scope.info[i].doc.address;
console.log($scope.info[i].doc);
});
}
}
};
在这里,我调用Select函数来选择一个用户,然后我想在输入中显示该用户,以便更新信息

<div class="col-xs-6 col-sm-3 sidebar-offcanvas" id="sidebar" role="navigation">
    <h3>All users</h3>
    <div class="list-group">
        <a href="#" ng-repeat="i in info" class="list-group-item" ng-click="select(i.doc._id)">{{i.doc.name + ' ' + i.doc.surname}}</a>
    </div>
</div>

所有用户
这里我使用$scope变量

<div class="col-xs-6 col-sm-3 sidebar-offcanvas" style="margin-left: 20%;">
    <h3>Selected user</h3>
    <input type="text" ng-model="sName"  class="form-control" placeholder="Name" />
    <input type="text" ng-model="sSurname" class="form-control" placeholder="Surname" />
    <input type="text" ng-model="sPhone" class="form-control" placeholder="Phone" />
    <input type="text" ng-model="sAddress" class="form-control" placeholder="Address" />
    <br/>
    <table>
        <tr>
            <td><button ng-click="" class="btn btn-lg btn-primary">Update</button></td>
            <td><label style="visibility: hidden;">a;dl</label></td>
            <td><button ng-click="" class="btn btn-lg btn-danger">Remove</button></td>
        </tr>
    </table>
</div>

选定用户

使现代化 A.dl 去除
$scope.sName,$scope.sName。。。在select函数之外未定义


有什么帮助吗?

您的问题是循环中有一个函数,因此执行函数时,
i
始终等于
$scope.info.length+1


如果您使用,它将警告您此类错误,以便您在运行时不会发现它们。

唯一的问题似乎是您正在使用
$scope.$apply()
回调到
ngClick
(这也会触发$digest循环)

删除
$scope.$apply()
,它应该可以正常工作:

$scope.select = function (id) {
    for (var i = 0; i < $scope.info.length; i++) {
        if (id === $scope.info[i].doc._id) {
            // $scope.$apply(function (){
                $scope.sName = $scope.info[i].doc.name;
                $scope.sSurname = $scope.info[i].doc.surname;
                $scope.sPhone = $scope.info[i].doc.phone;
                $scope.sAddress = $scope.info[i].doc.address;
                console.log($scope.info[i].doc);
            // });
        }
    }
};

你确定没有收到任何错误吗?没有,没有错误…我们需要查看更多代码。
$scope
的上下文是什么,以及
$scope是如何调用的。选择
调用的?我编辑了这篇文章,你可以看到我是如何使用$scope变量的。如果你告诉我你正在调用
$scope.$apply()
回调中单击
不会出现任何错误?很难相信:)这不是问题:/如果我删除$scope.$apply函数,此方法将设置$scope.sName、$scope.sSurname。。。变量,但我不能在方法之外使用它们。#问题是什么:/非常感谢:)@АаСаааааааааааааааааааааааа-答案左上角的箭头)。这样,其他面临类似问题的用户就会知道这个答案解决了它!
<a href="" ... ng-click="select(i.doc)" ng-repeat="i in info">...</a>

$scope.select = function (doc) {
    $scope.sName    = doc.name;
    $scope.sSurname = doc.surname;
    $scope.sPhone   = doc.phone;
    $scope.sAddress = doc.address;
};