2016-12-06 3 views
0

これは簡単な例です。私はこの質問のために書きましたが、ディレクティブを使用してディレクティブの属性にオブジェクトを渡すことができれば不思議です、テンプレート内のすべてtemplateUrl。私はそれが次のように動作すると思います:テンプレート機能でディレクティブを使用できますか?

angular.module('myModule') 
    .directive('someDirective', function() { 
    return { 
     scope: { 
     u: '=', 
     }, 
     template: '<avatar user="u"></avatar>', 
    }; 
    }); 

答えて

0

はいできます!定義の順序は関係ありません。

app.directive("directive1", function() { 
    return { 
     restrict: 'E', 
     scope: { 
      u: '@' 
     }, 
     template : "<h4>{{u}}</h4><directive2 user='{{u}}'></directive2>" 
    }; 
}); 
app.directive("directive2", function() { 
    return { 
     restrict: 'E', 
     scope: { 
      user: '@' 
     }, 
     template : "<h1>{{user}}</h1>" 
    }; 
}); 

このjsfiddle

+0

返信ありがとうございます!例を拡張して属性を渡すことができますか?好ましくはオブジェクト – Jesse

+0

確かに!それを今見てください。 – driconmax

+0

私は文字列を使って簡単にテストし結果を確認しました。オブジェクトを使用して '@'を '@'で変更し、何を印刷するかを変更したい場合( 'u.name'など) – driconmax

0

でアクションでそれを参照してください。はい、それはスコープ属性を通して、あなたの状態を渡すことは簡単です。この概念を示すplunkrがあります。

app = angular.module('app',[]) 
    .directive('someDirective', function() { 
    return { 
     scope: { 
     u: '=', 
     }, 
     template: '<avatar user="u"></avatar>', 
    }; 
    }).directive('avatar', function() { 
    return { 
     scope: { 
     user: '=', 
     }, 
     template: '<span>{{user.name}}</span>', 
    }; 
    }) 
関連する問題