简体   繁体   English

角度指令数据绑定

[英]angular directive data binding

I'm trying to write a simple Angular directive that displays an object properties, but I don't understand how the binding works. 我正在尝试编写一个显示对象属性的简单Angular指令,但是我不了解绑定的工作方式。

<div ng-repeat="c in cars">
   <div car carElement={{c}}></div>
<div>  

.directive('car', function() {
  return {
   template: function(elem, attr){
     var car = attr.carElement;
     return car.brand + ' ' + car.model; 
   }
 };
});

I can't bind the hole object, just simple properties. 我不能绑定孔对象,只能绑定简单的属性。 For example, this works: 例如,这有效:

<div ng-repeat="c in cars"> 
  <div car brand={{c.brand}} ></div> 
<div> 
.directive('car', function() { 
  return { 
    template: function(elem, attr){ 

    var brand = attr.brand; 
    return ''+brand; 
  } 
  }; 
});

How can I send the entire car to the directive template? 如何将整车发送到指令模板?

You have to pass the object, but not inside an expression: 您必须传递对象,而不是在表达式内部传递:

<div ng-repeat="c in cars">
   <div car carElement="c"></div>
<div>  

Directive: 指示:

.directive('car', function() {
  return {
   scope: { car: "=carElement" },
   template: function(elem, attr){
     //you now have 'car' in scope.car
   }
 };
});

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

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