I am currently using Angularjs and it's routeProvider to send partials or a view into my main page.
app.config(function ($routeProvider) {
$routeProvider
.when('/ball/:ball_id', {
templateUrl: '/ball.html',
controller: 'ballController'
});
})
.controller('ballController', function($scope, $rootScope) {
$scope.ball_id = $routeParams.ball_id;
});
ball.html (partial)
<div id="ball" ball_id="{{ball_id}}"></div>
<script>
// Want to wait for {{ball_id}} to be rendered.....
$(document).ready(function() {
// Read ball id and pass to function
console.log($('#ball').attr('ball_id')); // WANT #, but logs {{ball_id}} <<----
doSomethingBasedOnBallId($('#ball').attr('ball_id'));
});
function doSomethingBasedOnBallId(ballId) {
// calculate & make some other requests specific to only this page....
$('#ball').append(add-some-html);
}
</script>
Now before we go too crazy with separating models/views/etc... this is just one special case where the partial needs to know the 'ball_id' in order to perform additional actions.
So two questions:
Is there a way you can use {{ball_id}} in an actual javascript function in the ball.html page? ex:
doSomethingBasedOnBallId({{ball_id}});
Or do I need to do it the way I am doing it where I need to look it up in the DOM?My main problem is how in ball.html do I wait for {{ball_id}} to be rendered by angular? Under the document.ready(), the console just logs
{{ball_id}}
, although if I go through Chrome's js console and do $('#ball').attr('ball_id'), that works because at that point, angular has replaced the {{ball_id}} already.