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

时间:2014-04-02 10:32:46

标签: javascript angularjs angularjs-scope disqus

我在controller.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>

1 个答案:

答案 0 :(得分:0)

您可以使用$window服务

将其放在全局窗口对象上
app.controller('MainCtrl', function($scope, $window) {
    $window.postresult = 0;

    $scope.change = function() {
       $window.postresult++;  
    }
});

然后您可以通过window.postresult

的任何JavaScript访问它
window.setInterval(function() {
    window.$('#output').text(window.postresult);
},100);

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