javascript - 从指令 View 调用 Controller 中的函数,angularJS

标签 javascript html angularjs

我正在尝试使用 ng-click 来调用两个函数,在 html 中作为指令。第一个函数“checkUrl”位于指令 Controller 中,并且工作正常。

第二个函数“findUrl”位于主应用 Controller 中,并且未被触发。

HTML:

<div class="status" ng-click="checkUrl()">
    <b ng-click="checkUrl()">{{card.status}}</b>
</div>
<div ng-click="findUrl()" class="image">
    <img ng-src={{card.image}} alt="">
</div>

我有几个问题:

1)这是因为两个 Controller 具有不同的作用域,并且一个 Controller 无法访问另一个 Controller 吗?

2)如何获取指令 view/html 来调用主 Controller 功能?

指令的 Controller :

$scope.checkUrl = function() {
    console.log("invoking checkUrl from the directive's controller.")
    asnService.showUrl();
};

应用程序 Controller :

$scope.findUrl = function() {
    console.log("invoking findUrl from the app controller");
    asnService.showUrl();
};

最佳答案

要回答您的问题,您可以使用 $scope.$parent.findUrl() 访问指令父作用域上的作用域变量。不一定推荐,但它确实有效。

关于javascript - 从指令 View 调用 Controller 中的函数,angularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34999043/

相关文章:

javascript - 如何限制画线的位置

html - 密码管理 : Hard coded password in html

javascript - 将http拦截器写成类

javascript - 该对象在数组中,但indexOf找不到dit

angularjs - angular ui bootstrap dropup 不起作用

javascript - 用 PHPWord setValue 函数替换 html 实体

javascript - 我可以在不启动服务器的情况下使用 Ember JS 或其他框架吗?

javascript - YUI自动完成错位问题

html - 使用 css 内容属性和多选列表的冲突

javascript - 尝试随机选择一个短语