我有一個自定義指令,我想在頁面上的多個元素上使用。我無法隔離範圍並根據子範圍顯示元素。這裏是什麼,我試圖做一個例子:角度中的多個自定義指令範圍
app.js:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
})
.directive('comShowLabelOnFocus', function() {
return {
restrict: 'A',
scope: {
showlabel: '='
},
link: function(scope) {
scope.inputClicked = function() {
event.currentTarget.placeholder = '';
scope.showlabel = true;
};
}
}
});
的index.html:
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<div class="form-group medium-6 columns" com-show-label-on-focus>
<input
type="email"
name="email"
ng-model="model.email"
placeholder="Your Email Address"
ng-click="inputClicked()"/>
<label ng-show="showlabel">Email Address (Required)</label>
showlabel is: {{showlabel}}
</div>
<div class="form-group medium-6 columns" com-show-label-on-focus>
<input
type="text"
name="name"
ng-model="model.name"
placeholder="Your Name"
ng-click="inputClicked()"/>
<label ng-show="showlabel">Name (Required)</label>
showlabel is: {{showlabel}}
</div>
</body>
在行動:Plunker
基本上,領域標籤應該在該字段獲得焦點時出現。如果我在該指令中註釋掉該範圍聲明,那麼showlabel範圍變量就是根範圍,並且出現兩個標籤。謝謝。
我還將ng模型任務也移出了,以便data-my-model =「{{model.email}}」在我的模板中工作時執行:ng-model =「myModel」指示。非常感謝! – panzhuli