最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - make only one bold letter in a word ionic-angularjs - Stack Overflow

programmeradmin3浏览0评论

I would like to know how to set only a specific letter inside a word, in my case the first one, in angularJS. For example, I have an array of words like :

var Words = [Mon,Tue,Wed];

I want to show the first letter of every element bold and the rest of word with normal font.

I would like to know how to set only a specific letter inside a word, in my case the first one, in angularJS. For example, I have an array of words like :

var Words = [Mon,Tue,Wed];

I want to show the first letter of every element bold and the rest of word with normal font.

Share Improve this question edited Jun 3, 2015 at 12:43 mccainz 3,4975 gold badges36 silver badges46 bronze badges asked Jun 3, 2015 at 11:13 PhiceDevPhiceDev 5272 gold badges6 silver badges23 bronze badges 1
  • I think your question has a fundamental flaw in that this is not problem for AngularJS to solve but rather a fairly simple CSS issue. – mccainz Commented Jun 3, 2015 at 12:12
Add a ment  | 

3 Answers 3

Reset to default 6

Having you first letter bold is purely visual, so it should not be reflected in your HTML but purely managed by CSS. This is important for accessibility and usability of our text, also respect HTML semantics.

CSS is used to suggest its presentation to human users.

There is a property called ::first-letter (:first-letter) which work very well and is very well describe on MDN

The ::first-letter CSS pseudo-element selects the first letter of the first line of a block, if it is not preceded by any other content (such as images or inline tables) on its line.

Here is an exemple of CSS

p:first-letter {
    font-weight: bold;
}

And a JSFiddle to perform some test : http://jsfiddle/mtjzsh5v/

Hope this will work for you.

In the view, of course:

<div ng-repeat="value in Words">
    <b>{{value.substring(0,1)}}</b>{{value.substring(1)}}
</div>

Edit:

Using ::first-letter

<div ng-controller="MyCtrl">
  <div ng-repeat="Word in Words">
           <p>
             {{Word}}
           </p>
   </div>
</div>

Example: http://jsfiddle/kevalbhatt18/twvy0j4a/9/


If you want to bold entire word then you can use ng-if like this

Example: http://jsfiddle/kevalbhatt18/twvy0j4a/8/


Html:

<div ng-controller="MyCtrl">
  <div ng-repeat="Word in Words">
           <span ng-if="$index === 0" class="bold">{{Word}}- {{$index}}</span>
        <span ng-if="$index !== 0" class="preview">{{Word}}- {{$index}}</span>
   </div>
</div>

Controller:

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
    $scope.Words = ['Mon','Tue','Wed'];
}

CSS:

.bold{

    font-weight:bold;
}

发布评论

评论列表(0)

  1. 暂无评论