从包含ng的HTML访问父控制器

时间:2015-07-27 04:41:00

标签: javascript angularjs

我想从ng-included html访问控制器中的一个函数,其中包含带有html模板的指令。

含义,给定带控制器的父html模板:

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

profileCtrl:

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

myview.html:

<my-directive></my-directive>

myDirective:

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()">

profileCtrl:

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

1 个答案:

答案 0 :(得分:1)

试试这个

{{$parent.test}}