簡體   English   中英

從指令訪問父范圍

[英]Access parent scope from directive

我創建了一條指令,並嘗試訪問父作用域。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>

您應該使用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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM