ページに使用する2つの要素を定義する2つのディレクティブがあります。ページ上で両方のディレクティブを使用すると、最初のディレクティブのみがレンダリングされます。私は角度のいくつかのルールを壊していると確信していますが、確信していない。ディレクティブ経由の2つのカスタム要素、ページ上にレンダリングされるのは1つだけです
ディレクティブ:
//The template and handler for the set username button
chatApp.directive('setUsernameButton', ['username', function(username){
function link(scope, element, attrs){
element.bind('click', function(){
username.setUsername();
scope.$parent.$apply();
});
};
var template = '<div ng-disabled="username.invalidUsername" ng-class="{disabled: username.invalidUsername}" id="setNameButton" class="ui teal button">Set Name</div>'
return {
restrict: 'E',
replace: true,
template: template,
link: link,
scope: false
};
}]);
//The template and handler for username input field
chatApp.directive('usernameInputField', ['username', function(username){
function link(scope, element, attrs){
element.bind('keydown', function(e){
if(e.keyCode == 13){
username.setUsername();
scope.$parent.$apply();
}
});
};
var template = '\
<div class="ui input">\
<input ng-model="username.name" type="text" placeholder="Username...">\
</div>'
return {
restrict: 'E',
replace: true,
template: template,
link: link,
scope: false
};
}]);
HTMLスニペット:
<div ng-if="!username.nameSet" class="ten wide column">
<set-username-button />
<username-input-field />
</div>
を行うことができますが、私はJSFiddleに角度を使用することはできませんadib.mosharrof @ jsfiddle –
にサンプルのデモを提供してください可能性があり、それを使用しますか?私は非常に簡単な方法で私のモジュールにバインドするように見えることはできません。 'chatApp = angular.module(" chatApp "、[]);' '
' angleは '[$ injector:nomod]モジュール 'chatApp'を利用できません! 'エラーです。 –はい、私もそれを見ました.. codepenを使ってみてください。 あなたの指令に 'username'を注入していますが、ユーザ名のサービスはありません。 –