属性指令
Attribute directive in AngularJS
我试图创建一个属性指令,将在输入字段之前插入一个标签。从警告语句判断,html看起来是正确的。然而,我没有编译或做角元素正确。如有任何帮助,我将不胜感激。
小提琴是http://jsfiddle.net/2suL9/
这是JS代码。
var myApp = angular.module('myApp', []);
myApp.directive('makeLabel', function ($compile) {
return {
restrict: 'A',
replace: false,
link: function (scope, inputFld, attrs) {
var ForInput = attrs['name'];
var LabelSize = attrs['labelSize'];
var LabelText = attrs['makeLabel'];
var htmlStart = '<label for="' + ForInput + '" class="label-control ' + LabelSize + '">';
var htmlStar = '';
if (attrs['required'] ) {
htmlStar = '<span style="color:red">*</span>';
}
var htmlEnd = LabelText + ":</label> ";
var htmlTotal = htmlStart + htmlStar + htmlEnd;
alert(htmlTotal);
// Now add it before the input
var newLabel = angular.element(htmlTotal);
inputFld.prepend(($compile(htmlTotal)));
}
};
});
这是HTML
<!DOCTYPE html>
<html class="no-js" data-ng-app="myApp">
<head>
<title></title>
</head>
<body>
<div class="container">
<div class="row">
<form name="TestLabelForm" class="form-horizontal">
<div class="form-group">
<input type="text" name="Simple" required="" make-label="Test Label" label-size="col-md-7" />
</div>
</form>
</div>
<br />
Should look like
<br />
<div class="row">
<form name="ExampleForm" class="form-horizontal">
<div class="form-group">
<label for="Simple2" class="col-md-7"><span style="color:red">*</span>Test Label:</label>
<input type="text" name="Simple2" required="" />
</div>
</form>
</div>
</div>
<!-- Get Javascript -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"> </script>
<script src="js/TestLabelAttr.js"> </script>
</body>
</html>
看到被抛出的DOM Exception了吗?它找不到您创建的元素。您必须使用document.createElement()
,并通过$compile(scope)(newElement)
将新创建的元素引入作用域。这是一个工作小提琴:http://jsfiddle.net/2suL9/26/请注意,我没有实现你的例子的if条件,你必须添加它!
javascript部分如下所示:
var myApp = angular.module('myApp', []);
myApp.directive('makeLabel', function ($compile) {
return function (scope, inputFld, attrs) {
var el = inputFld[0];
var newEl = document.createElement("label");
newEl.setAttribute("for", attrs['name']);
newEl.setAttribute("class", "label-control");
var subEl = document.createElement("span");
subEl.setAttribute("style", "color:red");
subEl.innerHTML = "*";
var endText = document.createTextNode("Test Label:");
$compile(scope)(newEl);
$compile(scope)(subEl);
$compile(scope)(endText);
newEl.appendChild(subEl);
newEl.appendChild(endText);
inputFld.parent().prepend(newEl);
}
});
注意:改变指令之外的dom元素不是一个好的做法。应该为标签使用另一个指令,它应该显示在输入的前面。你可以在你的控制器中集中逻辑,并使用广播来通知指令,当他们应该改变。
相关文章:
- 具有{{}}指令属性的Angular
- AngularJs,在没有隔离的情况下将指令属性添加到作用域
- 如何使用指令属性值创建动态模板
- 通过指令属性传达操作
- AngularJS观察指令属性表达式,动态继承作用域
- 监视自定义指令属性在父作用域更改时不发生更改
- AngularJS:将$scope变量作为指令属性传递
- 指令 : 属性必须指定一个函数
- 将指令属性添加到指令元素
- 检查是否传递了指令属性
- 在 AngularJS 中计算指令属性中的表达式
- 将对象传递给 Angular 指令属性未通过
- AngularJS:无法从自定义指令属性中检索值以在自定义指令中解析
- 角度JS观察指令属性
- AngularJS指令属性在观察时不呈现值
- AngularJS:更改指令属性
- 正在访问控制器中的指令属性
- 在AngularJS中,当指令属性的作用域变量基于camelBase时,为什么它们需要用连字符分隔
- AngularJS将javascript对象绑定到指令属性
- 如何设置指令属性的默认值