Javascript 类型错误:$scope.apply 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28307345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:35:01  来源:igfitidea点击:

TypeError: $scope.apply is not a function

javascriptangularjs

提问by mzereba

I am trying to render the contacts list after retrieving it thru rdflib.js. It is loading properly and saving it in the list inside the scope.

我试图通过检索它后呈现联系人列表rdflib.js。它正在正确加载并将其保存在范围内的列表中。

But I am not able to render sine the $scope doesn't update, it seems like I am calling $scope.apply()in the wrong place. The error occurs because I call it outside angular, but I am deliberately calling it inside the function that is outside angular context (nowOrWhenFetched), so not making sense for me. any help?

但是我无法渲染正弦 $scope 不更新,似乎我$scope.apply()在错误的地方打电话。发生错误是因为我在 angular 之外调用它,但我故意在 angular 上下文 ( nowOrWhenFetched)之外的函数内部调用它,所以对我来说没有意义。有什么帮助吗?

$scope.load = function () {
    //$scope.getContactsList();
    var g = $rdf.graph();
    var f = $rdf.fetcher(g);

    f.nowOrWhenFetched($scope.path + '*',undefined,function(){

    var DC = $rdf.Namespace('http://purl.org/dc/elements/1.1/');
    var RDF = $rdf.Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
    var LDP = $rdf.Namespace('http://www.w3.org/ns/ldp#');
    //var myOntology = $rdf.Namespace('http://user.pds.org/ontology/'); 
    var VCARD = $rdf.Namespace('http://www.w3.org/2006/vcard/ns#');

    var evs = g.statementsMatching(undefined, RDF('type'), VCARD('Individual'));
    if (evs != undefined) {
        for (var e in evs) {
            var id = evs[e]['subject']['value'];
            var fullname = g.anyStatementMatching(evs[e]['subject'], VCARD('fn'))['object']['value'];
            var email = g.anyStatementMatching(evs[e]['subject'], VCARD('hasEmail'))['object']['value'];
            var phone = g.anyStatementMatching(evs[e]['subject'], VCARD('hasTelephone'))['object']['value'];

            var contact = {
                id: id.slice(id.length-1),
                Name: fullname,
                Email: email,
                Phone: phone 
            };

            $scope.contacts.push(contact);
        }

    }

    $scope.apply();

    });

};

回答by Michal Charemza

The function is $apply(), with the $.

函数是$apply(),带有$