2016-06-01 12 views
2

私のウェブサイトのすべてのページに表示されるdivがあります。角度指令 - 動的 "ページオプション"を作成する方法

私は、だから私は、私はディレクティブを使用する必要がありますことを決めた

(私はangular1ためのUIのルータを使用しています)現在の状態に応じてどこにでもあるが、異なるデータでそれを表示したいです。

pageoptions.html

<div class="page-options"> 
    <!-- if state = 'dashboard' --> 
    <h1>Dashboard</h1> 
    <a ui-sref="restore">Some Action</a> 
    <a ui-sref="backup">Another Action</a> 

    <!-- if state = 'Edit' --> 
    <h1>Edit</h1> 
    <a ui-sref="restore">Delete</a> 

    <!-- if state = 'settings' --> 
    <h1>Settings</h1> 
    <a ui-sref="backup">Settings</a> 
</div> 

pageoptions-directive.js

myApp.directive('pageinfo', function() { 
    return { 
     restrict: 'A', 
     templateUrl: "./components/pageinfo/pageinfo.html" 
    }; 
}); 

しかし、私はコメントで書いたロジックを挿入する方法がわかりませんHTMLで

私はこの

+0

あなたは 'pageinfo.html'ファイルでなければならないのは何を? – georgeawg

答えて

1

ng-hideまたはng-showを使用して、あなたの指示に$ stateサービスを注入し、マークアップでそれを使って現在の状態を確認できるはずです。うまくいけば、これはかなり近いあなたを取得:

myApp.directive('pageinfo', ['$state', function($state) { 
    return { 
     restrict: 'A', 
     templateUrl: "./components/pageinfo/pageinfo.html" 
     link: function(scope) { 
      scope.currentState = $state.current.name; 
     } 
    }; 
}); 

テンプレート:

<div class="page-options"> 
    <!-- if state = 'dashboard' --> 
    <div ng-show="currentState == 'dashboard'"> 
    <h1>Dashboard</h1> 
    <a ui-sref="restore">Some Action</a> 
    <a ui-sref="backup">Another Action</a> 
    </div> 

    <div ng-show="currentState == 'Edit'"> 
    <!-- if state = 'Edit' --> 
    <h1>Edit</h1> 
    <a ui-sref="restore">Delete</a> 
    </div> 

    <div ng-show="currentState == 'settings'"> 
    <!-- if state = 'settings' --> 
    <h1>Settings</h1> 
    <a ui-sref="backup">Settings</a> 
    </div> 
</div> 
+0

正解:)ありがとうございました。私はすでに 'link:function(scope、element、attrs){scope.state = attrs.state;}を使用しました。 } 'もうまく動作します – user3800799

-1

にいくつかのアドバイスを得るのが大好きだ多分これは役立つだろう:

directive.js:

myApp.directive('pageinfo', function() { 
    return { 
     restrict: 'EA', 
     scope: { 
      state: '=' // if you want take a sting in directive , use '@' 
     }, 
     templateUrl: "./components/pageinfo/pageinfo.html" 
    }; 
}); 

template.html:

<div class="page-options"> 
    <!-- if state = 'dashboard' --> 
    <div ng-show="state == 'dashboard'"> 
     <h1>Dashboard</h1> 
     <a ui-sref="restore">Some Action</a> 
     <a ui-sref="backup">Another Action</a> 
    </div> 

    <!-- if state = 'Edit' --> 
    <div ng-show="state == 'Edit'"> 
     <h1>Edit</h1> 
     <a ui-sref="restore">Delete</a> 
    </div> 

    <!-- if state = 'settings' --> 
    <div ng-show="state == 'settings'"> 
     <h1>Settings</h1> 
     <a ui-sref="backup">Settings</a> 
    </div> 
</div> 

使用ディレクティブこのように:

<pageinfo state="get your state and put it in here"></pageinfo> 
+0

何らかの理由でng-showは常にfalseを返します。私はあなたのコードを正確に使用します – user3800799

+0

この例は、 'pageinfo'指示文が要素指示文として使用されていることを示していますが、コードはそれを属性指示文としてのみ使用するように制限しています。 – georgeawg

+0

属性ディレクティブとして使用するには、どのような変更を適用する必要がありますか? – user3800799

関連する問題