2016-11-22 12 views
1

要素のテキスト内容にアクセスできるAngularJS要素指示文を作成します。例えば、私がこのように使用することができますディレクティブを作成したいと思います:要素テキストの内容を表示するAngularJS指示文

<my-text-input g-model="something" g-placeholder="Enter some text">My label</my-text-input> 

は、どのように私は上記のMy labelテキストへのアクセスを得ることができますか? 私は、次の例を作成しました:

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>AngularJS Element</title> 
 
    <!-- AngularJS --> 
 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script> 
 

 
</head> 
 
<body ng-app="myApp"> 
 

 
<div ng-controller="MyCtrl"> 
 
    <h1>Directive Example</h1> 
 
    <my-text-input g-label="My label" g-model="something" g-placeholder="Enter some text"></my-text-input> 
 
    <br> 
 
    <!-- I really want to use it like this: --> 
 
    <my-text-input g-model="something" g-placeholder="Enter some text">My label</my-text-input> 
 
    <p>Text value: {{something}}</p> 
 
</div> 
 

 
<script> 
 
    var app = angular.module('myApp',[]); 
 

 
    app.controller('MyCtrl', ['$scope', function($scope) { 
 
     $scope.something= "Example" 
 
    }]); 
 

 
    app.directive('myTextInput',function(){ 
 
     return { 
 
      restrict: 'E', 
 
      template: '<div>' + 
 
      '<label>{{gLabel}}</label> ' + 
 
      '<input ng-model="gModel" type="text" placeholder="{{gPlaceholder}}">' + 
 
      '</div>', 
 
      scope: { 
 
       gLabel : "@", 
 
       gModel : "=", 
 
       gPlaceholder : "@?" 
 
      } 
 
     }; 
 
    }); 
 

 
</script> 
 
</body> 
 
</html>

答えて

0

だから、これはNG-transcludeがためですまさにです表示されます。

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>AngularJS Element</title> 
 
    <!-- AngularJS --> 
 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script> 
 
</head> 
 
<body ng-app="myApp"> 
 

 
<div ng-controller="MyCtrl"> 
 
    <h1>Directive Example</h1> 
 
    <my-text-input g-model="something" g-placeholder="Enter some text">My label</my-text-input> 
 
    <p>Text value: {{something}}</p> 
 
</div> 
 

 
<script> 
 
    var app = angular.module('myApp',[]); 
 

 
    app.controller('MyCtrl', ['$scope', function($scope) { 
 
     $scope.something= "Example" 
 
    }]); 
 

 
    app.directive('myTextInput',function(){ 
 
     return { 
 
      restrict: 'E', 
 
      transclude : true, 
 
      template: '<div>' + 
 
      '<label ng-transclude></label> ' + 
 
      '<input ng-model="gModel" type="text" placeholder="{{gPlaceholder}}">' + 
 
      '</div>', 
 
      scope: { 
 
       gModel : "=", 
 
       gPlaceholder : "@?" 
 
      } 
 
     }; 
 
    }); 
 

 
</script> 
 
</body> 
 
</html>

:私は私の例を更新しました
関連する問題