简体   繁体   English

角度指令中琐碎但令人困惑的JavaScript

[英]Trivial but confusing javascript in angular directive

The author of this tutorial describes the following code as such: "every element of stars array has an object with value 'filled' that is evaluated to true or false based on the value of scope.ratingValue that we get from the DOM." 本教程的作者将这样描述以下代码:“ stars数组的每个元素都有一个值为'filled'的对象,该对象根据从DOM获得的scope.ratingValue的值被评估为true或false。”

directive('fundooRating', function () {
  return {
    restrict: 'A',
    template: '<ul class="rating">' +
              '<li ng-repeat="star in stars" class="filled">' +
                  '\u2605' +
              '</li>' +
            '</ul>',
    scope: {
      ratingValue: '='
    },
    link: function (scope, elem, attrs) {
      scope.stars = [];
      for(var i = 0; i < scope.max; i++) {
      scope.stars.push({filled: i < scope.ratingValue});
  }
} 

As a js newbie, I still can't make heads or tails on how to "read" that last line. 作为一个js新手,我仍然无法在如何“阅读”最后一行上走得通。 From the code the only place where filled is mentioned that I can see is the css file and the template here. 从代码中,提到的唯一可以填充的地方是css文件和此处的模板。 So there is no bool value that I can see and the syntax is a bit confusing for me. 因此,我看不到bool值,语法对我来说有点混乱。 How should i think about it? 我应该怎么想呢? github code github代码

I checked the source and the template says: 我检查了源,模板说:

'<li ng-repeat="star in stars" ng-class="star" ng-click="toggle($index)">'

When the ng-class directive is given an object, it checks every one of its properties, and if it's truhty, it adds the property name as a classname. 当为ng-class指令提供一个对象时,它会检查其每个属性,如果它是真实的,则会将属性名称添加为类名称。 Exemple: 例:

ng-class="{filled:true, bold:false}" will evaluate class="filled" . ng-class="{filled:true, bold:false}"将评估class="filled"

So this last line sets the 'filled' status of the star, and so the filled class will be put only to the scope.max -nth elements. 因此,这最后一行设置了星形的“填充”状态,因此填充类将仅放置到scope.max -nth元素中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM