Javascript AngularJS在使用ng开关渲染后执行
我创建了一个ng开关,用于隐藏和显示“页面”,对于一些需要运行jQuery选择器函数的元素。但是由于Angular会破坏并创建渲染元素,因此我需要一种重新应用jquery选择器函数的方法 我的第一次尝试只是在元素中包含一个标记,但它只在页面第一次呈现时执行它 更新: HTML:Javascript AngularJS在使用ng开关渲染后执行,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我创建了一个ng开关,用于隐藏和显示“页面”,对于一些需要运行jQuery选择器函数的元素。但是由于Angular会破坏并创建渲染元素,因此我需要一种重新应用jquery选择器函数的方法 我的第一次尝试只是在元素中包含一个标记,但它只在页面第一次呈现时执行它 更新: HTML: 这在textarea上似乎不起作用,但我在这里使用了以下代码: 要创建此项,请执行以下操作: 关键部分定义了一个指令: directive("floatLabels", function(){ return {
这在textarea上似乎不起作用,但我在这里使用了以下代码: 要创建此项,请执行以下操作: 关键部分定义了一个指令:
directive("floatLabels", function(){
return {
restrict: "A",
link:function(scope, iElem, iAttrs){
iElem.floatlabel()
console.log(iElem)
}
}
})
<input
type="text"
class="form-control"
placeholder="Last Name"
float-labels/>
应用该指令:
directive("floatLabels", function(){
return {
restrict: "A",
link:function(scope, iElem, iAttrs){
iElem.floatlabel()
console.log(iElem)
}
}
})
<input
type="text"
class="form-control"
placeholder="Last Name"
float-labels/>
我不确定下面的代码是否有效,但让我解释一下背后的原因。 正如shaunhusain在评论中提到的,如果您创建一个指令并将其放置在ng开关内,它将在ng开关后处理。现在,您可以知道jquery是在处理ng开关之后执行的。您的指令可能希望排除一些标记,这样您就不必每次都使用相同的模板 现在创建指令:(可能有一两个输入错误,但我希望它能有所帮助!)
angular.module('myModule',[])
.directive('myDirective',function(){
返回{
限制:'E',
是的,
模板:“”+“”+“”+“”+“”+“”,
,
链接:功能(范围、el、属性){
$('.float-label控件').floatLabels();
}
};
});
名字
姓
文本区域
如果我们能看到一些代码,回答这个问题会更容易。是否希望每次ng开关更改时都运行jquery选择器函数?更新了原始值。如果需要访问某些DOM元素,则需要使用指令来执行此操作,它将消除这个问题,因为只要angular在DOM中看到该指令,就会重新处理该指令。您可以链接到您正在使用的floatLabels javascript函数吗?我发现了一个floatlabel库,但它似乎使用了不同的语法
angular.module('myModule', [])
.directive('myDirective', function() {
return {
restrict: 'E',
transclude: true,
template: '<div class="step-content">' + '<form role="form">' + '<div ng-transclude></div>' + '</form>' + '</div>',
,
link: function(scope, el, attr) {
$('.float-label-control').floatLabels();
}
};
});
<myDirective>
<div class="form-group float-label-control">
<label for="first_name">First Name</label>
<input type="email" class="form-control" placeholder="First Name" name="first_name" ng-model="currentCase.customer.first_name">
</div>
<div class="form-group float-label-control">
<label for="">Last Name</label>
<input type="text" class="form-control" placeholder="Last Name">
</div>
<div class="form-group float-label-control">
<label for="">Textarea</label>
<textarea class="form-control" placeholder="Textarea" rows="1"></textarea>
</div>
</myDirective>