2017-01-31 5 views
0

Я создал директиву, и я пытаюсь получить доступ к родительской области. Выбранный элемент 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> 
+0

Вы должны пройти 'test1' значение директивы через свойство директивы области видимости. Как выглядит шаблон директивы? – Dario

+0

Спасибо, я добавил шаблон директивы выше – user6934713

+0

где '$ scope.SelectedItemChange' в шаблоне директивы? –

ответ

1

Вы должны использовать scope: true

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; 
 
     } 
 
    } 
 
});
<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>

Смежные вопросы