如何在javascript中使用Angular JS变量

时间:2016-06-03 12:08:07

标签: javascript html angularjs variables scope

我正在研究一个项目,我必须使用一个函数来调用Angular JS ng-repeat中的朋友,并且该函数是用javascript编写的,所以我很困惑,我想传递属于angular js的id variablr叫做c.Parent.id 我想在javascript函数中使用它,这是我的函数

 <script>

  $(document).ready(function()
  {
        var start=/@/ig;
        var word=/@(\w+)/ig;

    $("# -->>> HERE I WANT TO USE THAT ANGULAR JS VARIABLE <<<--").live("keyup",function() 
    {
        var content=$(this).text();
        var go= content.match(start);
        var name= content.match(word);
        var dataString = 'searchword='+ name;

        if(go.length>0)
        {
            $("#msgbox").slideDown('show');
            $("#display").slideUp('show');
            $("#msgbox").html("Type the name of someone or something...");
            if(name.length>0)
            {
                $.ajax({
                        type: "POST",
                        url: "<?php echo base_url(); ?>index.php/ItemDetail/getfriends",
                        data: dataString,
                        cache: false,
                        success: function(html)
                        {
                            $("#msgbox").hide();
                            $("#display").html(html).show();
                        }
                });

            }
        }
        return false();
    });

    $(".addname").live("click",function() 
    {   

        var username=$(this).attr('title');
        var id=$(this).attr('name');
        var old=$("#685").html();
        var content=old.replace(word,""); 
        $("# -->>> HERE I WANT TO USE THAT ANGULAR JS VARIABLE <<<--").html(content);
        var E="<a class='red' contenteditable='false' href='<?php echo base_url(); ?>index.php/user/wall/"+id+"' >"+username+"</a>";
        $("# -->>> HERE I WANT TO USE THAT ANGULAR JS VARIABLE <<<--").append(E);
        $("#display").hide();
        $("#msgbox").hide();
        $("# -->>> HERE I WANT TO USE THAT ANGULAR JS VARIABLE <<<--").focus();
    });

});

</script>

2 个答案:

答案 0 :(得分:1)

我认为你可以这样做。

var scope = angular.element($("#outer")).scope();
    scope.$apply(function(){
        scope.msg = 'Superhero';
    })

来源:AngularJS access scope from outside js function

答案 1 :(得分:0)

$ scope变量仅在AngularJS Controller中可用。如果你真的需要将jQuery逻辑与AngularJS混合,那么你可以将变量的值赋给全局变量。

var globalScope = {};

app.controller('MyCtrl',function($scope)) {
   globalScope.prentId = $scope.c.Prent.id;
}

但我的建议是将你的应用程序逻辑放在AngularJS控制器中,不要将它与jQuery混合使用。