12

There are several questions like this in stackoverflow. I know. Tried all the answers, but still no luck. My html :

    <div class="headline" id="headline-game">
        {{gameContent.headline}}
    </div>

jQuery:

    var country = $("#headline-game").scope().headline;
    alert(country);

But I got undefined, instead of my scope. Can anyone help me? I don't wanna change the scope, just accessing it.

1
  • 1
    jQuery has no scope() function. Commented Feb 19, 2014 at 10:04

4 Answers 4

14

Angular adds a global angular variable to your window.

So you can do something like this:

angular.element("#headline-game").scope().gameContent.headline;
Sign up to request clarification or add additional context in comments.

6 Comments

I also did that before. got an error said: TypeError: Cannot read property 'headline' of undefined
do you have gameContent object? try just this angular.element("#headline-game").scope() on the console and examine the available properties
@hjgrace yes I do. I tried that. but maybe because I tried in console after the $scope loaded. and the jquery function was called before it was loaded. do u have an idea to solve it?
@catwoman one way is to break execution, with breakpoinst in chrome console or write "debugger;" (without quotes) after you set your gameContent variable on your controller so it breaks execution and then you can inspect your scope in the console.
it does not works when $compileProvider.debugInfoEnabled(false);
|
2

The issue is caused by the order in which the plugins are initialized.

  1. jQuery
  2. AngularJS

So accessing scope() in a jQuery script on document load will result in undefined, since jQuery is run before Angular.

The solution to this is to let AngularJS execute jQuery once it is ready, using directives.

Example :

Controller

app.directive("myDirective", function() {
        return {
            restrict: "A",
            scope: {
                customheadline: "@"
            },
            link: function(scope, elem, attrs) {
                var country = scope.customheadline;
                alert(country);
            }
        }
    });

HTML

<div class="headline" id="headline-game" my-directive customheadline="{{ gameContent.headline }}">
    {{gameContent.headline}}
</div>

1 Comment

What did work for me is: app.directive("myDirective", function() { return { restrict: "A", scope: { customheadline: "@" }, link: function(scope, elem, attrs) { var country = element.scope().customheadline; alert(country); } } });
0

Try this

var country = $("#headline-game").scope().gameContent.headline;
alert(country);

4 Comments

I did, but because of scope().gameContent is undefined I got an error said: TypeError: Cannot read property 'headline' of undefined
So I think you are trying to access it before it defines.
you are right. maybe i tried to access it before it defines, eventhough i wrote it inside $(function () {}) Then what is the right way to do it?
You need to use that jquery piece of code inside Angular something like in a $scope.$watch("gameContent.headline") to make sure you have the scope and value in place.
0

Assume the model value is return from a $http request.

In the controller you have 3 functions:

function getGameContentSuccessCallback(){
   doSuccessCallback();
}

function getGameContentErrorCallback(){
  doSth()
}

function getGameContent(){
  GameContentService.GetGameContent(submitData,getGameContentSuccessCallback,getGameContentErrorCallback)

}

then you define doSuccessCallback in jQuery

var doSuccessCallback = function(){   
   var country  = angular.element("#headline-game").scope().gameContent.headline;   
   alert(country );   
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.