2017-08-15 21 views
1

我正在學習angularJS中的組件模式。我正嘗試在組件模式中使用綁定。我使用的綁定名稱是名字。但問題是它沒有約束力。當我用abc代替名字它工作正常。誰能說出可能導致這個問題的原因?AngularJS組件模式綁定是否具有特定的命名格式?

分量代碼是如下:

(function() { 
 
     angular.module('mainModule').component('heroList', { 
 
      template: '<p>Hello from component {{$ctrl.firstName}}</p>', 
 
      //controller: 'HeroListController', 
 
      bindings: { 
 
       firstName: '=' 
 
      } 
 
     }) 
 

 
    }());

的index.html,然後index.js調用此組分是:

(function() { 
 
     var app= angular.module('mainModule', []); 
 

 
     app.controller('myCtrl', function() { 
 
      var vm = this; 
 
      vm.firstName = "John"; 
 
      vm.lastName = "Doe"; 
 
     }) 
 
    
 
    
 
    }())
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>Angular Components</title> 
 
    <script src="scripts/angular.min.js"></script> 
 
     <script src="index.js"></script> 
 
     <script src="heroes/hero-list.component.js"></script> 
 
     <script src="heroes/hero-list.controller.js"></script> 
 

 
</head> 
 
<body ng-app="mainModule" ng-controller="myCtrl as vm"> 
 

 
    {{vm.firstName}} {{vm.lastName}} 
 

 
    <hero-list firstName="vm.firstName"><hero-list> 
 

 
</body> 
 
</html>

輸出我得到的是:

> John Doe 
> 
>Hello from component 

但經過我更換的firstNameABC我得到所需的輸出。

>John Doe 
> 
>Hello from component John 

任何人都可以幫我解決這個問題嗎?

回答

1

綁定使用與組件名稱相同的camelCase符號,例如它們使用破折號分隔。因此,要使用firstName綁定,請將其用作:

<hero-list first-name="vm.firstName"><hero-list> 
相關問題