First time using angularJS here. Basically, i used the routing service, so i have a with routing calling in a template with html code in it. I also have all these regular javascript functions that are required for the code in the template to display properly (slideshows and among many other things).
When i run i get all these null errors (it seems that my JS functions cant find various elements because either it cant access code in the ng-view/template or it got run before ng-view/template got loaded).
When i paste all of my javascript functions in the controller that i assigned for that template, it worked. But i have a feeling im not supposed to do that. What is the correct way to make this work? Thanks.
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider){
$routeProvider
.when("/",{
templateUrl:"about.html",
controller:"aboutCtrl"
})
.when("/aboutus", {
templateUrl:"entertainment.html"
})
});
app.controller('aboutCtrl', function($scope){
//placed regular javascript/jquery code here to make it work
});
Your jQuery functions probably don't work, because the elements on which they shall be executed can't be found when you are on a different route.
Ways to fix that:
Options 2 & 3 will work, but will also quickly turn your app into a huge mess. I would definately recommend to dig more into angular directives, to keep your code clean.
Maybe this tutorial could be a good starting point for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With