从指令访问父范围

时间:2017-01-31 12:13:23

标签: javascript angularjs

我创建了一个指令,我正在尝试访问父作用域.SelectedItemChange没有启动,我无法访问Test1的值。

app.directive('driverNotes', function ($location) {
    return {
        restrict: 'E',
        transclude: true,
        templateUrl: "../HtmlTemplates/notesTempl.html",
        controller: function ($scope, $http) {

            console.log($scope.$parent.Test1);

            $scope.SelectedItemChange = function(item) {
                if (item != undefined || item != null) {
                    console.log(1);
                }
            }
        }
    }
});

指令模板

<md-input-container class="notesTable" flex-gt-sm ng-disabled="userForm.$invalid" ">
<table class="notesTable">
    <tr><th>Description</th><th>Date</th><th>Down</th><th>Premium</th><th>Date Updated</th></tr>
    <tr ng-repeat="note in notesSearch">

    </tr>
</table>
</md-input-container>

1 个答案:

答案 0 :(得分:1)

您应该使用scope: true

&#13;
&#13;
var app = angular.module('myApp', []);

app.controller("parentCtrl", function ($scope) {
    $scope.name = 'daddy';
});

app.directive('child', function ($location) {
    return {
        restrict: 'E',
        template: 'I am a child of {{parentName}}',
        scope: true,
        controller: function ($scope) {
            $scope.parentName = $scope.$parent.name;
        }
    }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp">
  <div ng-controller="parentCtrl as vm">
    <child></child>
  </div>
</div>
&#13;
&#13;
&#13;

相关问题