I am currently learning AngularJS and attempting to transform my webpage using the "angular way" instead of relying on jQuery. However, I am facing some challenges in understanding how to achieve this.
This is how my HTML looks:
<body style="padding-top: 0px;" data-spy="scroll" ng-app="SummerMill">
<section id="intro" class="main style1 dark">
<!-- Header -->
<header ng-controller="MainController" id="header">
<!-- Logo -->
<h1 id="logo">Summer Mill</h1>
<a ng-mouseover="locations()"
style="color:black;text-decoration:initial;"
id="logoii"
href="http://localhost/locations">Locations</a>
<!-- Nav -->
<nav id="nav">
<ul class="nav navbar-nav">
<li ng-repeat="headerLink in headerLinks"><a ng-init="content=headerLink.text" href="#{{content}}">{{content}}</a></li>
</ul>
</nav>
</header>
Below is my controller script:
app.controller('MainController', ['$scope', function($scope) {
$scope.headerLinks = [
{
text: 'Intro',
alternativeText: 'Arlington'
},
{
text: 'Wholesale',
alternativeText: 'New York'
}
];
$scope.locations = function() {
content = headerLinks.alternativeText;
}
}]);
Essentially, I want the content inside the <li>
element to change when hovered over. While the hover event is triggered correctly, I receive an error stating ReferenceError: headerLinks is not defined. This seems strange as it is declared in the controller itself. I attempted to resolve this by changing
content = $scope.headerLinks.alternativeText;
, which removed the error but it appears that the content
variable in the controller is different from the content
variable used in ng-init
.
Could you guide me on the correct approach to accomplish this task? It's possible that I may be approaching this problem the wrong way.