javascript - 从 ng 包含的 HTML 访问父 Controller

标签 javascript angularjs

我想从 ng 包含的 html 访问 Controller 中的函数,其中包含带有 html 模板的指令。

含义,给定带有 Controller 的父 html 模板:

<div ng-controller="profileCtrl">
    <div ng-include src="profileContent"></div>
</div>

个人资料Ctrl:

$scope.profileContent = '/html/views/myview.html';
$scope.test = 'This should show';

myview.html:

<my-directive></my-directive>

我的指令:

angular
    .module('myModule')
    .directive('myDirective', [function () {
        return {
            restrict: 'E',
            templateUrl: '/html/directives/myDirectiveTemplate.html',
            ...

myDirectiveTemplate.html:

{{test}} //should output "This should show"

如何从子 myDirective 访问 $scope.test?

<小时/>

myDirectiveTemplate.html:

<form ng-submit="$parent.updateProfile()">

个人资料Ctrl:

    $scope.updateProfile = function() {
        console.log('updating profile'); //not called

最佳答案

尝试这样

{{$parent.test}}

关于javascript - 从 ng 包含的 HTML 访问父 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31645097/

相关文章:

javascript - 加载自定义模型并将字段添加到销售点中的 pos.order.line - Odoo 12

javascript - getElementByClass 像 getElementById 这样跨浏览器使用是否安全?

angularjs - 获取 JSON 格式的 Azure Blob 数据

javascript - 通过第一个列表中项目的索引将项目添加到第二个列表

javascript - 需要禁用下拉菜单的淡出功能(仅)

javascript - 如何在 ASP.NET 中动态地将数据附加到 JQuery 控制变量?

javascript - Angular 待办事项列表不起作用

angularjs - 在两个 Controller 之间共享服务的数据

angularjs - UI-Router 1.0 基于用户角色重定向

javascript - JQuery 如何处理 getElementsByClassName 迭代?