Ng-repeat not passing down certain attributes without quotes to scope

时间:2017-08-30 20:11:21

标签: javascript angularjs angularjs-directive

I am attempting to pass down attributes returned from a MongoDB query to a directive in Angular, but for some reason it will not pass down an "_id" attribute. When I view the records returned in the parent's $scope, I can see that each object does in fact have an "_id". However when I go to inspect the $scope of the directive I am attempting to render, I can see that it has every attribute besides "_id".

My parent template code:

a

My directive code:

b

Is there something special about including an attribute that begins with an underscore?

2 个答案:

答案 0 :(得分:2)

It should have worked, only wrap your attribute value with <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/> <xsl:template match="/nearme"> <table border="1"> <!-- a row for every group of 5 items --> <xsl:for-each select="location[position() mod 5 = 1]"> <tr> <!-- a cell for every item in the current group of 5 --> <xsl:for-each select=". | following-sibling::location[position() &lt; 5]"> <td> <xsl:value-of select="."/> </td> </xsl:for-each> </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet> (quotes)

Rather than passing each value inside attribute, you could consider passing whole card object to directive.

<table border="1">
  <tr>
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>D</td>
    <td>E</td>
  </tr>
  <tr>
    <td>F</td>
    <td>G</td>
    <td>H</td>
    <td>I</td>
    <td>J</td>
  </tr>
  <tr>
    <td>K</td>
    <td>L</td>
  </tr>
</table>

Directive

#!/usr/bin/perl

use Proc::ProcessTable;

$ref = new Proc::ProcessTable;

foreach $proc (@{$ref->table}) {
  if(@ARGV) {
    next unless grep {$_ == $proc->{pid}} @ARGV;
  }

  print "--------------------------------\n";
  foreach $field ($ref->fields){
    print $field, ":  ", $proc->{$field}, "\n";
  }
}

So inside directive you could easily access value inside " object and <div id=cards> <div ng-repeat='card in cards' class='card'> <card-directive my-card="card"></card-directive> </div> </div> ensure one way binding. You could use function cardDirective() { return { restrict: 'E', scope: { 'myCard': '<' }, for using card.

答案 1 :(得分:0)

AngularJS正在使用大写_id将属性Id规范化为I

有关详细信息,请参阅AngularJS Developer Guide - Directive Normalization

&#13;
&#13;
angular.module("app",[])
.directive("customDirective", function() {
  return {
    template: `Id={{Id}}`,
    link: function(scope, elem, attrs) {
      scope.Id = attrs.Id;
      console.log("Id=",attrs.Id);
    }
  }
})
&#13;
<script src="//unpkg.com/angular/angular.js"></script>
  <body ng-app="app">
    <div custom-directive _id=99></div>
  </body>
&#13;
&#13;
&#13;