当试图从控制器更新视图时,AngularJS未定义变量
AngularJS Undefined Variables When Trying To Update View From Controller
我是新来的AngularJS和我试图重写我的网页在"角的方式",而不是使用jquery,但我遇到了一些问题与我的理解。
我的HTML是这样的:
<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>
然后我的控制器:
app.controller('MainController', ['$scope', function($scope) {
$scope.headerLinks = [
{
text: 'Intro',
alternativeText: 'Arlington'
},
{
text: 'Wholesale',
alternativeText: 'New York'
}
];
$scope.locations = function() {
content = headerLinks.alternativeText;
}
}]);
基本上,在悬停时,我想改变<li>
的内容。我知道悬停事件正在被触发,而且是正确的。我得到的错误是ReferenceError: headerLinks未定义,这对我来说很奇怪,因为它在控制器本身,所以我尝试了content = $scope.headerLinks.alternativeText;
,并且停止了错误,但我猜控制器中的content
与ng-init
中的content
不一样。
正确的方法是什么?也许我想错了。
对于您的情况,最好稍微简化您的代码,例如使用ng-if
和标志isOver
angular.module('app',[])
.controller('MainController',function($scope){
$scope.headerLinks = [
{
text: 'Intro',
alternativeText: 'Arlington'
},
{
text: 'Wholesale',
alternativeText: 'New York'
}
];
$scope.locations = function() {
content = headerLinks.alternativeText;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!-- Header -->
<header ng-app="app" ng-controller="MainController" id="header">
<!-- Logo -->
<h1 id="logo">Summer Mill</h1>
<a ng-mouseover="isOver=true" ng-mouseout="isOver=false"
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-if="!isOver" href="#{{headerLink.text}}">{{headerLink.text}}</a>
<a ng-if="isOver" href="#{{headerLink.alternativeText}}">{{headerLink.alternativeText}}</a>
</li>
</ul>
</nav>
</header>
你也可以像这样用三元运算符代替ng-if
angular.module('app',[])
.controller('MainController',function($scope){
$scope.headerLinks = [
{
text: 'Intro',
alternativeText: 'Arlington'
},
{
text: 'Wholesale',
alternativeText: 'New York'
}
];
$scope.locations = function() {
content = headerLinks.alternativeText;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!-- Header -->
<header ng-app="app" ng-controller="MainController" id="header">
<!-- Logo -->
<h1 id="logo">Summer Mill</h1>
<a ng-mouseover="isOver=true" ng-mouseout="isOver=false"
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 href="#{{!isOver ? headerLink.text : headerLink.alternativeText}}">{{!isOver ? headerLink.text : headerLink.alternativeText}}</a>
</li>
</ul>
</nav>
</header>
,甚至没有条件,如
angular.module('app', [])
.controller('MainController', function($scope) {
$scope.currentText = 'text';
$scope.headerLinks = [{
text: 'Intro',
alternativeText: 'Arlington'
}, {
text: 'Wholesale',
alternativeText: 'New York'
}];
$scope.locations = function() {
content = headerLinks.alternativeText;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!-- Header -->
<header ng-app="app" ng-controller="MainController" id="header">
<!-- Logo -->
<h1 id="logo">Summer Mill</h1>
<a ng-mouseover="currentText='alternativeText'" ng-mouseout="currentText='text'" 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 href="#{{headerLink[currentText]}}">{{headerLink[currentText]}}</a>
</li>
</ul>
</nav>
</header>
相关文章:
- $window.ga在AngularJS事件中未定义
- this.router在AngularJS 2中未定义
- AngularJS指令出错-无法读取属性'编译'的未定义
- AngularJS指令单元测试中未定义的函数
- angularjs无法读取未定义的属性then
- AngularJS和promise值在调用本地函数时的效果-未定义
- AngularJS:'$范围未定义'
- Angularjs-NgTable在重新加载时未定义
- AngularJS-参数'菜单Ctrl'不是函数,未定义
- AngularJs无法读取属性“”;UpdateTime”;的未定义
- AngularJS自定义过滤器未触发点击事件
- 未定义的控制器AngularJS
- AngularJS ng repeat显示原型函数未定义
- angularjs-ui路由器解析工厂未定义
- Angularjs类型错误:无法读取属性'GET()'的未定义
- AngularJs 检查是否未定义和空的最佳实践
- 参数不是函数-AngularJS未定义
- AngularJS未定义的函数
- 当属性双向绑定时,Angularjs未定义对象
- 当试图从控制器更新视图时,AngularJS未定义变量