Javascript 如何在父级上使用ng show发出指令以触发show上的函数
我有一个有ng秀的节目。这个div是我创建的自定义指令的包装器 HTML 我想让指令做点什么,只要从包装器div中删除ng hide类(换句话说,只有“在显示中”) 我该怎么做 谢谢大家! AngularJS ng show指令 如果表达式的计算结果为true,则Javascript 如何在父级上使用ng show发出指令以触发show上的函数,javascript,jquery,html,css,angularjs,Javascript,Jquery,Html,Css,Angularjs,我有一个有ng秀的节目。这个div是我创建的自定义指令的包装器 HTML 我想让指令做点什么,只要从包装器div中删除ng hide类(换句话说,只有“在显示中”) 我该怎么做 谢谢大家! AngularJS ng show指令 如果表达式的计算结果为true,则ng show指令将显示指定的HTML元素,否则HTML元素将隐藏。 干得好。我已经为您提供了一个AngularJS中的自定义指令的示例,如果status value等于true,它将显示一行句子。希望这对你有帮助。干杯 var-a
ng show
指令将显示指定的HTML元素,否则HTML元素将隐藏。
干得好。我已经为您提供了一个AngularJS中的自定义指令的示例,如果status value等于true,它将显示一行句子。希望这对你有帮助。干杯
var-app=angular.module(“myApp”,[]);
app.controller(“appCtrl”,函数($scope){
$scope.status=true;
});
app.directive(“myDirective”,function()){
返回{
限制:“A”,
模板:“由指令生成!”
};
});代码>
有趣!下面是我想解决这个问题的方法:
我会像这样添加ng show
:
ng-show="mycondition && doSomething()"
现在,它将在您的条件满足时调用doSomething
函数,即只要ng show
是真实的
好极了,让我们将此函数添加到myDirective
中,然后我们将console.log
记录在函数中,以了解它是否被调用。像这样:
app.directive("myDirective", function() {
return {
restrict: "A",
scope: {
doSomething: "="
},
template: "<h5>inside myDirective!</h5>",
controller: function($scope) {
$scope.doSomething = function() {
console.log("myDirective doing something");
return true; // Important, this function being a condition of ng-show needs to return true
}
}
};
});
切换
太棒了。谢谢有一件事我不能100%理解:为什么我需要将doSomthing传递给指令?@Yogev你说我希望指令做一些事情
,所以为了在指令内部执行,我们需要将它传递给指令。
ng-show="mycondition && doSomething()"
app.directive("myDirective", function() {
return {
restrict: "A",
scope: {
doSomething: "="
},
template: "<h5>inside myDirective!</h5>",
controller: function($scope) {
$scope.doSomething = function() {
console.log("myDirective doing something");
return true; // Important, this function being a condition of ng-show needs to return true
}
}
};
});
<div ng-show="mycondition && doSomething()">
<div my-directive do-something="doSomething"></div>
</div>