2015-09-28 21 views
0

我在做一個非常簡單的指令,這是app.js代碼很簡單角指令使用限制,以「C」不工作

var myApp = angular.module('myApp', []) 

myApp.directive('layout-top', function() { 
    return { 
     restrict: 'C', 
     templateUrl: 'templates/layout__top.template.html' 
    } 
}) 

這是模板加載:

<div class="layout__top"> 
    <div class="top__left"> 
     Some kind of content 
    </div> 
</div> 

而且THI,是html代碼...

<!DOCTYPE html> 
<html lang="en" ng-app="myApp" ng-strict-di> 
    <head> 
     <meta charset="UTF-8"> 
    </head> 
    <body ng-controller="MainCtrl"> 
     <div class="layout__wrapper"> 
      <!-- TOPBAR GOES HERE --> 
      <div class="layout-top"></div> 
     </div> 
     <script type="text/javascript" src="bower_components/angular/angular.js"></script> 
     <script type="text/javascript" src="js/app.js"></script> 
    </body> 
</html> 

當我嘗試使用restric到A或E可以工作,但不能和班級一起工作。爲什麼?

+4

難道不該

? –

+0

我認爲Caleb可能是正確的,下劃線應該是一個連字符... – hally9k

+2

也是一個雙下劃線,可以與單一的 – charlietfl

回答

1

不應使用class="layout-top"兩次(在你的模板中的HTML代碼各一次)

更改您的模板中的類從class="layout-top"class="layout-top-tmp"

+0

很好的... OP改變了有問題的代碼......它讓人困惑......新鮮的眼睛幫助 – charlietfl

+0

謝謝夏洛! – Nicolas2bert

+0

@NicolasHumbert你是對的!這是因爲名稱轉換'layout__top'轉換爲'layoutTop'? – Sergio

0

你有

<div class="layout-top"></div> 

你需要

<div layout-top></div> 
+1

否... OP使用'restrict:' C'' ...你正在顯示'限制:'A'' – charlietfl

+0

@charlietfl正是...我想限制它到類...限制它到A或E的作品,但我想嘗試Class .. – Sergio

0

尼古拉斯對重複呼叫是正確的。

但是不應該在指令聲明中指令的名稱是「layoutTop」嗎?

myApp.directive('layoutTop', (...)) 
+0

我拼錯了代碼。現在被糾正! – Sergio

+0

是問題嗎? –

+0

抱歉,我不能今天鍵入:(問題是模板中的'layout__top',它被Angular轉換爲'layoutTop',並與html部分中的'layoutTop'發生衝突,因爲我輸入了答案對不起。 – Sergio