2016-03-24 15 views
1

什麼,我試圖做的是創建下面的標記:簡單的指令來工作就像一個綁定表達式

<current-user /> 

這個指令應該簡單地注入當前用戶名稱就像一個綁定表達式{{} currentUser.name }

這裏是我,但我在最後的caret失去了我的跨度標籤:

HTML:

<a href="" class="dropdown-toggle" data-toggle="dropdown"> 
    <i class="fa fa-user"></i> 
    <current-user /> 
    <span class="caret"></span> 
</a> 

的Javascript:

app.directive('currentUser', function ($rootScope, auth) { 
    return { 
     restrict: 'E', 
     transclude: true, 
     compile: function (elem) { 
      $rootScope.$watch('auth.profile', function (profile) { 
       if (profile) { 
        elem.html(profile.email); 
       } 
      }); 
     } 
    } 
}); 

任何幫助,將不勝感激

+1

啊...'$ rootScope。在'compile'指令$ watch',爲什麼呢? &我也可以知道爲什麼'elem.html'作爲電子郵件將是簡單的文本.. –

+0

假裝我沒有使用rootcope和用戶名可能是任何東西。我只是選擇電子郵件。 – Marco

回答

1

這是一個known issue在一些瀏覽器。

使用非自閉指令解決問題。

<a href="" class="dropdown-toggle" data-toggle="dropdown"> 
    <i class="fa fa-user"></i> 
    <current-user></current-user> 
    <span class="caret">Test</span> 
    </a> 

Working Demo

相關問題