如何在用我的HTML编写的javascript中访问scope.postresult值

How can I access scope.postresult value in javascript written in my HTML?

本文关键字:javascript 访问 scope postresult 我的 HTML      更新时间:2023-09-26

我有下面的控制器.js代码,其中我得到了"postresult"值。

$scope.open= function(post) {
           $scope.postresult = post;
}

在下面的HTML代码中,我正在加载DISQUS线程,我需要访问"postresult"对象,我该怎么做?

<script type="text/javascript">
    var pid = '{{postresult.id}}';
    var pid1 = 'postresult.id';
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'a2b'; // required: replace example with your forum shortname
    var disqus_identifier =  '{{postresult.id}}' ;
    var disqus_title = '{{postresult.postText}}';
    var disqus_url = 'http://pingle.com:8080/ngdemo/{{postresult.id}}';
    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
    var reset = function (postresult.id, http://pingle.com:8080/ngdemo/{{postresult.id}}, postresult.postText, en) {
        DISQUS.reset({
            reload: true,
            config: function () {
                this.page.identifier = newIdentifier;
                this.page.url = newUrl;
                this.page.title = newTitle;
                this.language = newLanguage;
            }
        });
    };
</script>
您可以使用

$window服务将其放在全局窗口对象上

app.controller('MainCtrl', function($scope, $window) {
    $window.postresult = 0;
    $scope.change = function() {
       $window.postresult++;  
    }
});

然后你可以从任何JavaScript访问它,window.postresult

window.setInterval(function() {
    window.$('#output').text(window.postresult);
},100);

下面是一个示例:http://plnkr.co/edit/sj69pvrpHGk2gFl4xDJZ?p=preview