$ scope不在回调函数之外

时间:2014-07-01 22:06:10

标签: javascript angularjs pouchdb

我开发了与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 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);
                });


            }
        }

    };

这里我调用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 variables

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

$ scope.sName,$ scope.sSurname ...在select函数之外是未定义的。

任何帮助?

2 个答案:

答案 0 :(得分:0)

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

如果您使用jshint,它会警告您出现此类错误,因此您无法在运行时发现这些错误。

答案 1 :(得分:0)

唯一的问题似乎是你在$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);
            // });
        }
    }
};

另请参阅此 short demo


额外奖励1:

当链接href#时,大多数浏览器都会滚动到页面顶部(除非您手动阻止它)。如果您希望<a>表现为按钮并仍然被设为链接,则应使用href=""


额外奖励2:

您不必通过id然后lopp通过i中的所有info来查找匹配的doc。 您可以直接传递相应的doc

<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;
};