admin管理员组文章数量:1360170
I'm trying to give an app the ability to set the font for portion of text based on a list of fonts from Google Fonts.
Here's what I have:
<ul ng-init='headerFont="mono"'>
<li ng-repeat='font in fonts' style='font-family: {{font}};' ng-click='headerFont = font'>{{font}}
</li>
</ul>
and then later on:
<h1 style='font-family: {{headerFont}};'>Header</h1>
and in the controller in my js file:
$scope.fonts = [...an array of font names...]
Originally, instead of ng-repeat
on <li>
, I was using:
<select ng-model='headerFont' style='font-family: {{headerFont}};'>
<option ng-repeat='font in fonts' style='font-family:{{font}};'>{{font}}
</option>
</select>
And that worked exactly like I wanted it to. But I wanted to try using a scrollable list instead of a drop-down menu because the <select>
menu on mobile browsers doesn't support changing fonts of individual options, and it's important to me for the fonts to be able to be previewed before choosing. I figured I could just use ng-click
to set the value of headerFont
, but it doesn't do anything (no errors show up in the console or anything. Just nothing happens.) Does anyone know why?
I'm trying to give an app the ability to set the font for portion of text based on a list of fonts from Google Fonts.
Here's what I have:
<ul ng-init='headerFont="mono"'>
<li ng-repeat='font in fonts' style='font-family: {{font}};' ng-click='headerFont = font'>{{font}}
</li>
</ul>
and then later on:
<h1 style='font-family: {{headerFont}};'>Header</h1>
and in the controller in my js file:
$scope.fonts = [...an array of font names...]
Originally, instead of ng-repeat
on <li>
, I was using:
<select ng-model='headerFont' style='font-family: {{headerFont}};'>
<option ng-repeat='font in fonts' style='font-family:{{font}};'>{{font}}
</option>
</select>
And that worked exactly like I wanted it to. But I wanted to try using a scrollable list instead of a drop-down menu because the <select>
menu on mobile browsers doesn't support changing fonts of individual options, and it's important to me for the fonts to be able to be previewed before choosing. I figured I could just use ng-click
to set the value of headerFont
, but it doesn't do anything (no errors show up in the console or anything. Just nothing happens.) Does anyone know why?
-
2
It would be much easier if you could provide a jsFiddle or plunkr or something with your question. Anyway I think you can use
ng-style
directive and don't use statements in your declarative code (HTML), instead make that one line of code a method in your controller, it would be much easier to debug or follow later. – Kia Raad Commented Jul 26, 2015 at 7:44
2 Answers
Reset to default 8The problem is that ngRepeat creates child scope for each iteration, so ngClick actually changes local child headerFont
variable.
One possible solution is to explicitly refer parent scope:
var app = angular.module('demo', []);
app.controller('demoController', function($scope) {
$scope.fonts = ['Arial', 'Times New Roman', 'Verdana', 'mono'];
})
<script data-require="[email protected]" src="https://code.angularjs/1.4.3/angular.js" data-semver="1.4.3"></script>
<div ng-app="demo" ng-controller="demoController">
<h1 style='font-family: {{headerFont}};'>Header</h1>
<ul ng-init='headerFont="mono"'>
<li ng-repeat='font in fonts' style='font-family: {{font}};' ng-click='$parent.headerFont = font'>{{font}}</li>
</ul>
</div>
Each ng-repeat
creates a new scope. The ng-click
changes the headerFont
in its own scope, without touching the headerFont
on the top level.
You can create dictionary at the top level to prevent the child scope from kicking in:
<ul ng-init="top = {'headerFont':'mono'}">
<li ng-repeat='font in fonts'
style='font-family: {{font}};'
ng-click='top.headerFont = font'>
{{font}} / {{top.headerFont}}
</li>
</ul>
Demo: http://plnkr.co/edit/77zesZsBgYpZUDWciY18?p=preview
版权声明:本文标题:javascript - AngularJS: Why won't ng-click set a variable as a value obtained from ng-repeat? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743929446a2563572.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论