我是 MVC 和 Angular 的新手。
我有两个 Controller 和两个关联的 View ,exampleview1.cshtml/ts 和 exampleview2.cshmtl/ts。
在 exampleview1.cshtml 中,我有一个按钮,它使用 Angular 指令“ng-click”来调用 exampleview1.ts 中的函数。
在 exampleview2.cshmtl 中,我有一个包含一些文本的段落。此段落包含在一个 div 中,该 div 使用“ng-if”指令调用 exampleview2.ts 中最初隐藏该 div 的函数。这是使用 jQuery 的 .hide() 方法完成的。
我希望 exampleview1 中的按钮与 exampleview2 中的文本绑定(bind)。单击此特定按钮时,它应该将 exampleview2 中的文本设置为可见(可能使用 jQuery 的 .toggle() 或 .show() 方法)。
如何设置它,以便当单击按钮时,它会像平常一样调用与其关联的事件,同时还告诉 exampleview2.ts 中的函数从 .hide() 切换到 .show()?目前,与“ng-if”指令相关的函数如下所示:
viewScope.testingText = function () {
$('#divcontainstext').hide();
return true;
};
所以目前它被迫隐藏,但我不确定是否需要更改此函数以适应 .show() 行等。我不确定如何将这些函数绑定(bind)在一起。
最佳答案
在 example2.html 中使用 ng-show 指令,该指令附加了一个范围值,用于告诉它 true 或 false。
并在单击时从 example1.html 内部广播一个事件,以便当在 example2.html 内监听此事件时,它会更改 ng-show 的作用域变量值.
example1.html
<button ng-click="change()">Click </button>
app.controller('example1Ctrl,function($rootScope,$scope){
$scope,change=function(){
$rootScope.broadcast('visibleStatus','true');
}
}
example2.html:
<div ng-show="visible">//some contents that needs to be hidden/shown
</div>
app.controller('example2Ctrl',function($rootScope,$scope){
$scope.visible=false;
$rootScope.$on('visibleStatus',function(event,data){
$scope.visible=data;
}
}
这可能不是要复制和运行的确切代码,但尝试此方法并且可能会使用您自己的相关代码。
关于javascript - MVC 中的函数可以跨 Controller 交互吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30435021/