【问题标题】:Event onclick in my controller我的控制器中的事件 onclick
【发布时间】:2015-06-12 13:06:22
【问题描述】:

我在控制器中编写了这段代码:

$('#categoryId').on('click', function(event){       
        $scope.items = $scope.getUpdatedItems ();
        console.log('Items  ',$scope.items );
    });

在我的指令中我有这个:

....
scope.$watch('items', function(items){
  console.log('Directives - Items  ',items );
....

问题是我从不输入指令中声明的 $watch。

有人可以帮助我.. 谢谢

【问题讨论】:

标签: angularjs angularjs-directive controller scope


【解决方案1】:

您不应该将 jQuery 与 AngularJS 混为一谈,这会扰乱 Angular 的摘要循环。 Angular 确实提供了自己的指令来处理此类活动,例如在您的情况下,您可以使用 ng-click 指令,这将在单击该元素时调用 ng-click 属性值中提到的函数。

标记

<button type="categoryId" ng-click="updateAllItems()"></button>

代码

$scope.updateAllItems = function(){       
    $scope.items = $scope.getUpdatedItems ();
    console.log('Items  ',$scope.items );
});

上面的代码将调用位于控制器内部的updateAllItems 函数,然后$scope.items 将得到更新。然后你看里面的指令会被调用。

【讨论】:

  • @user880386 对您有帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多