标签的属性在角度方向上不起作用
Attribute of label not working in angular directive?
我正在努力学习如何使用角度指令,到目前为止取得了成功。我只有一个小问题无法解决。
在我的指令中,我在输入字段的id的相同值上获得了一个for属性集。但是,点击标签并不能使输入控件像正常工作那样聚焦。
我在一些示例代码中解决了这个问题:
<div ng-app='test' ng-controller='testCtrl'>
<label for="test1">Testlabel 1</label><br>
<input id="test1" type="text"></input><br>
<my-input id="test2"
label="Testlabel 2"
placeholder="enter somthing"
text="testVar"></my-input><br>
<span>{{testVar}}</span>
</div>
和javascript:
angular.module('test', [])
.directive('myInput', function() {
return {
restrict: 'E',
template: '<label for={{id}}>{{label}}</label><br>' +
'<input id={{id}} type="text" ' +
' placeholder={{placeholder}} ng-model="text" />',
scope: {
id: "@",
label: "@",
placeholder: "@",
text: "="
}
}
})
.controller('testCtrl', ['$scope', function($scope) {
$scope.testVar = 'testing';
}]);
jsfiddle中的相同代码:http://jsfiddle.net/U92em/
我犯了什么错误导致了我的问题,我该如何解决?
您的"包装器"也有相同的id
,这并不好。您可以在link
函数中删除它,方法如下:
link: function(scope,el,attrs){
el.removeAttr("id");
}
工作:http://jsfiddle.net/cherniv/5u4Xp/
或者在compile
功能中(感谢Florent):
compile: function(el){
el.removeAttr("id")
}
示例:http://jsfiddle.net/cherniv/7GG6k/
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 监视函数从服务返回不起作用,但作用域函数起作用
- 幻灯片滚动javascript不起作用
- 简单的javascript在Shopify中不起作用
- Recaptcha在IE7和IE8中不起作用
- Ember Data DS.Model's set函数不起作用
- JsFiddle上的鼠标事件不起作用
- 我的AngularJS表达式没有'不起作用
- 点击按钮输入不起作用
- 面向对象的Javascript代码在IE7中不起作用
- 分部隐藏在jquery中不起作用
- 在phonegap应用程序内部重定向不起作用
- Array.length似乎不起作用;console.log则显示其他情况
- $ionicplatform内的$scope不;不起作用
- 我的javascript for循环不起作用
- Meteor-添加用户自定义字段的方法不起作用
- 为什么 .focus() 不起作用,而 .css(“color”,“red”) 在同一个选择器上起作用
- 标签的属性在角度方向上不起作用
- 谷歌方向don'不起作用
- 谷歌地图API的方向服务;不起作用