我正在努力学习如何使用角度指令,到目前为止成功.我只有一个小问题,我无法弄清楚.
在我的指令中,我在输入字段的id的相同值上设置了for属性.但是单击标签并不会使输入控件成为正常工作的焦点.
我在一些示例代码中解决了这个问题:
{{testVar}}
和javascript:
angular.module('test', []) .directive('myInput', function() { return { restrict: 'E', template: '
' + '', 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/