[英]Change scope variable from directive in angularJS
I am pretty new with Angular and I got the following problem: 我对Angular相当陌生,遇到以下问题:
I have a list view and a details view with some tags. 我有一个列表视图和一个带有一些标签的详细信息视图。 I created a directive in order to change the route (to list view) and load new elements from a service on the click event.
我创建了一条指令以更改路由(到列表视图)并在click事件上从服务加载新元素。 What I want is that the click event also change a scope variable, so that i can show/hide a button to clear filters in the list view.
我想要的是click事件还更改了作用域变量,以便我可以显示/隐藏按钮以清除列表视图中的过滤器。
Actually I am able to change the variable from the directive, but the button doesn't show/hide with ngIf/ngShow/ngHide when the variable changes. 实际上,我可以从指令中更改变量,但是当变量更改时,按钮不会显示/隐藏ngIf / ngShow / ngHide。
I tried with $scope.$apply
, both in the directive and in the controller. 我尝试在指令和控制器中使用
$scope.$apply
。 I tried calling a toggle
function from the controller and to change the variable directly inside the directive, but nothing worked. 我尝试从控制器调用
toggle
功能,并直接在指令中更改变量,但没有任何效果。
I tried calling the toggle function from another button and it worked perfectly! 我尝试从另一个按钮调用切换功能,效果很好!
Here is my code: 这是我的代码:
Here i try to change the variable in the directive and $apply
: 在这里,我尝试更改指令和
$apply
的变量:
.directive('filterTag', function(getFeed, $location) {
return {
restrict: 'A',
controller: 'mainController',
link: function(scope, element, attrs) {
element.bind('click', function() {
$location.path('/list');
scope.filtered = true;
scope.$apply();
return getFeed.getTagged(this.text);
});
}
}
})
This is inside the controller: 这在控制器内部:
$scope.filtered = false;
$scope.toggleFiltered = function() {
$scope.filtered = !$scope.filtered;
}
$scope.$watch('filtered', function() {
alert('filtered: ' + $scope.filtered);
});
I use $watch to be sure the variable changed correctly and it does, both from directive and from toggle button. 我使用$ watch来确保从指令和切换按钮都正确地更改了变量并做到了。
Here is the button i want to show/hide if filters are applied (this is in the list view): 如果要应用过滤器,这是我要显示/隐藏的按钮(在列表视图中):
<button type="button" ng-show="filtered">Clear filter</button>
I tried to add this button to check if I was missing something or for syntax errors, but it works perfectly 我试图添加此按钮以检查是否缺少某些内容或语法错误,但是它可以正常工作
<button type="button" ng-click="toggleFiltered()">toggle filter</button>
I looked for answers in the last two days but I couldn't find the solution. 最近两天我一直在寻找答案,但找不到解决方案。 Can't figure out the problem with my code.
无法找出我的代码存在的问题。
you can share the 'filtered' between two controllers (different scopes), so you can use $rootScope
to do that or you can use a service/factory
in this way when the 'filtered' is changed in any controller it will change in the other! 您可以在两个控制器(不同作用域)之间共享“已过滤”,因此可以使用
$rootScope
来执行此操作,或者在任何将更改“已过滤”控制器的控制器中使用“ service/factory
”其他!
Use the $rootScope.filtered instead of $scope.filtered and you can initialize the $rootScope.filtered in the run() function at the module. 使用$ rootScope.filtered而不是$ scope.filtered,可以在模块的run()函数中初始化$ rootScope.filtered。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.