2016-12-04 99 views

回答

1

在第一片段中,我替换@以便它取值的结合。如果您使用=绑定,它期望您传入变量。

在第二个片段中,我使用了=绑定,但使用ng-int创建了变量。

angular 
 
     .module('client', []) 
 
     .component('testComponent', { 
 
      template: '<h1>{{$ctrl.param}}</h1>', 
 
      controller: function() { 
 
       // this.param = '123'; 
 
      }, 
 
      bindings: { 
 
       param: '@' 
 
      } 
 
     }) 
 
     .component('heroDetail', { 
 
      template: '<span>Name: {{$ctrl.hero}}</span>', 
 
      controller: function() {}, 
 
      bindings: { 
 
       hero: '@' 
 
      } 
 
     });
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="UTF-8"> 
 

 
    <title>AngularJS Example</title> 
 

 
    <!-- AngularJS --> 
 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script> 
 

 

 
</head> 
 
<body> 
 

 
<div ng-app="client"> 
 
    Test 
 
    <test-component param = "Test123"> </test-component> 
 
    <hero-detail hero="Superman"></hero-detail> 
 
</div> 
 

 
</body> 
 
</html>

使用摘录=绑定。

angular 
 
     .module('client', []) 
 
     .component('testComponent', { 
 
      template: '<h1>{{$ctrl.param}}</h1>', 
 
      controller: function() { 
 
       // this.param = '123'; 
 
      }, 
 
      bindings: { 
 
       param: '=' 
 
      } 
 
     }) 
 
     .component('heroDetail', { 
 
      template: '<span>Name: {{$ctrl.hero}}</span>', 
 
      controller: function() {}, 
 
      bindings: { 
 
       hero: '=' 
 
      } 
 
     });
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="UTF-8"> 
 

 
    <title>AngularJS Example</title> 
 

 
    <!-- AngularJS --> 
 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script> 
 

 
</head> 
 
<body> 
 

 
<div ng-app="client" ng-init="testParam= 'Test123'; testHero='Superman'"> 
 
    Test 
 
    <test-component param = "testParam"> </test-component> 
 
    <hero-detail hero="testHero"></hero-detail> 
 
</div> 
 

 
</body> 
 
</html>