角指令字段不需要括号 [英] Angular directive fields not require brackets
问题描述
我使用的UI引导的日期选择器功能,我想获得阿霍德传递到一个属性值之一。
// JS
$ scope.myMaxDate =新的日期();<! - HTML - >
<输入日期选择器,弹出=MM / DD / YYYYMAX-日期=myMaxDate/>
我不明白,为什么在这种情况下, MAX-日期
ATTR需要一个字符串,而不是前pression像 {{ myMaxDate}}
。它是如何获得实际值的保持?
更重要的是,我用一个装饰来改变从该指令的一些数据,并希望访问此属性,但我得到的是字符串 myMaxDate
。
$ provide.decorator(datepickerPopupDirective,[$委托功能($代表){
//获取的指令和旧的链接函数引用
变种指令= $代表[0];
VAR链接= directive.link; //使用编译一个新的链接功能
directive.compile =功能(){
//我们要返回新的链接功能
返回功能(范围,元素,ATTRS,ngModelCtrl){
的console.log(attrs.maxDate); //'myMaxDate //调用旧的链接功能
link.apply(这一点,参数);
};
};
要回答你的日期选择器,弹出
指令如何找到<$ C的实际值问题$ C> MAX-日期属性,它最有可能使用范围的
。 $ EVAL
法
因此,在您code,看实际值使用:
的console.log($范围的eval(attrs.maxDate));
这也是为什么指令并不需要双大括号。其实双大括号会引起问题,因为它将你的 myMaxDate
对象转换为字符串,并因此失去了日期
对象方法。
有关 $ EVAL
方法看的 AngularJS范围API
I'm using UI Bootstrap's datepicker function and I'd like to get ahold of one of the values passed into an attribute.
// JS
$scope.myMaxDate = new Date();
<!-- HTML -->
<input datepicker-popup="MM/dd/yyyy" max-date="myMaxDate" />
I don't understand why in this case the max-date
attr takes a string rather than an expression like {{myMaxDate}}
. How is it getting a hold of the actual value?
What's more, I'm using a decorator to alter some data from this directive and would like to access this attribute but all I get is the string myMaxDate
.
$provide.decorator("datepickerPopupDirective", ["$delegate", function($delegate) {
// get references to the directive and old link function
var directive = $delegate[0];
var link = directive.link;
// create a new link function using compile
directive.compile = function() {
// the new link function we want to return
return function(scope, element, attrs, ngModelCtrl) {
console.log(attrs.maxDate); // 'myMaxDate'
// invoke the old link function
link.apply(this, arguments);
};
};
To answer your question on how the datepicker-popup
directive finds the actual value of the max-date
attribute, it most likely uses the $eval
method of the scope
.
So in your code, to see the actual value use:
console.log(scope.$eval(attrs.maxDate));
This is also why the directive doesn't require double curly brackets. In fact double curly brackets will cause problems because it will convert your myMaxDate
object to a string and consequently lose the Date
object's methods.
For more information on the $eval
method look at the AngularJS Scope API
这篇关于角指令字段不需要括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!