1

Please see the code here http://plnkr.co/edit/FqfkcyZSqPkA7JjMMLrb?p=preview

I am embedding a javascript object/value in html, which needs to be read by angular. It reads value in index.html, but not in partial. (_global_link is read properly, but not _global_link_partial). Is it because _global_link_partial not available at $routeChangeSuccess, if so which event I need to listen to. I could provide the value as const in module definition, or directly in controller etc, but this value is very view specific and better to maintain there.

Thanks.

error:

_global_link Object {link: "abc"} controllers.js:3
ReferenceError: _global_link_partial is not defined

code:

function Test1Ctrl($scope) {
$scope.$on('$routeChangeSuccess', function ($event, current) {
    console.log('_global_link', _global_link);
    console.log('_global_link_partial', _global_link_partial);
  });
}
function Test2Ctrl($scope) {
  $scope.$on('$routeChangeSuccess', function ($event, current) {
    console.log('_global_link', _global_link);
    console.log('_global_link_partial', _global_link_partial);  });
}

index.html

<div class="container">
    <ul>
      <li><a href="test1">test1</a>
      <li><a href="test2">test2</a>
    </ul>
        <div ng-view></div>
</div>

<h2>in index.html</h2>
<script>
  _global_link = {link: 'abc'}
</script>

partials (test1, test2)

<h2>In test1</h2>

<script>
  _global_link_partial = {link: 'link1'}
</script>

<h2>In test2</h2>

<script>
  _global_link_partial = {link: 'link2'}
</script>

1 Answer 1

1

I short, you need to include jQuery before angular.js

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>

Take a look at @igor's answer at https://groups.google.com/forum/?fromgroups=#!topic/angular/H4haaMePJU0 :

long story short: it's because the script tag is special and angular doesn't treat it as such. if you include jquery on this page, the code should work.

When angular detects jquery, it will use it for dom manipulation and jquery is smart enough to treat the script tag as special.

Here is a plunker: http://plnkr.co/edit/UhKx8QWGTExLgdQMJuyi?p=preview

I changed your example plunker to latest version of angular.js (1.2.13) and also changed the routes a little.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 tanks for pointing to the correct resource and the working example. still, I wish a better approach as so far I didn't need jquery, and just for this!
@bsr You try to use the global namespace with inline scripts. well This is definitely a bad practice, I saw people using ngInit for such things but I would never throw data on my templates. Just make a resolve on the route to get the data injected into your controllers
may be you can suggest a better way on what I am doing. say, I need to declare some value specific to a view. If I have 500 pages, I need to declare that many (minimum) in my module. but, if it is only used by that view, then if I include it in the view, the load time improves and easier to maintain. what do u think?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.