2016-12-20 45 views
0

HTML代碼:一個指令範圍屬性綁定到一個字符串

<ul drag-drop idProperty="today" > 
    <li draggable="true" ng-repeat="option in options track by $index" > 
     {{option.plan_option_id}} 
    </li> 
</ul> 

的DragDrop是一個自定義指令:

module.directive('dragDrop', function() { 
    return {  
    scope: { 
     idProperty:'@' 
    }, 
    ... 
    } 
}); 

我想爲idProperty得到的只是一個字符串值(例如星期二)沒有連接到$範圍(例如$ scope.today ='Tuesday')。反正有沒有綁定idProperty 直接在HTML中的字符串值?類似的東西:idProperty="'Tuesday'"

+0

使用'ID屬性= 「今天」而不是 – devqon

回答

2

您應該使用id-property="today"代替,使這項工作。

爲了得到它,而不使用該指令的scope,你可以只讀取指令的屬性:

module.directive('dragDrop', function() { 
    return { 
    link: function(scope, element, attrs) { 
     var idProperty = attrs.idProperty; 
    } 
    } 
}); 
2

你非常接近。

你有這樣一行:

<ul drag-drop idProperty="today" > 

這應該是

<ul drag-drop id-property="today" > 
0

大概應該是這樣的

<ul drag-drop id-property="today" > 
<li draggable="true" ng-repeat="option in options track by $index" > 
    {{option.plan_option_id}} 
</li> 

module.directive('dragDrop', function() { 
return {  
scope: { 
    idProperty:'@' 
}, 
link: function(scope,elem, attrs, ctrl){ 
     var idProperty = null; 
     attrs.$observe('idProperty', function(val){ 
      var idProperty = val 
     }) 
    } 
} 
}); 
0

時你在你的html中使用指令,那麼它必須被使用像這樣使用

ngRepeat directive 
in html - > ng-repeat 


your directive which you have made 
idProperty 
in html -> id-property 

,因爲每一個資本性質應與<替代 - (smallcharacter)> 所以使用

<ul drag-drop id-property="today"> 

閱讀本文件

https://docs.angularjs.org/guide/directive