0
我想從指令的鏈接或控制器函數中訪問ng-repeat中的數據。我的指令代碼如下所示。指令AngularJs中的鏈接函數的訪問範圍?
app.directive('ssLeftNavDirective', function($rootScope) {
// Runs during compile
return {
templateUrl: 'leftnavmenu.html',
scope: {
displayMode: '='
},
controller: function($scope) {
$scope.tests = ['health check', 'Performance Monitor', 'Compare Test', 'API Helpdesk'];
$scope.troubleshooting = ['Trouble Shooting 1', 'Trouble Shooting 2', 'Trouble Shooting 3'];
$scope.help = ['Help 1', 'Help 2', 'Help 3'];
$scope.settings = ['Settings 1', 'Settings 2', 'Settings 3'];
},
link: function($scope, iElm, iAttrs, controller) {
$scope.menuDisplayed = true;
$scope.tests = ['health check', 'Performance Monitor', 'Compare Test', 'API Helpdesk'];
$scope.troubleshooting = ['Trouble Shooting 1', 'Trouble Shooting 2', 'Trouble Shooting 3'];
$scope.help = ['Help 1', 'Help 2', 'Help 3'];
$scope.settings = ['Settings 1', 'Settings 2', 'Settings 3'];
}
};
});
我將它添加到控制器和鏈接中,以測試它。
這是該指令的模板:
<div id="menudiv">
<nav id="leftnavmenu" ng-class="menuDisplayed ? 'navon' : 'navoff'">
<a href="#" ng-repeat="nav in displayMode">{{nav}}</a>
</nav>
<i class="fa hamburgermenu" aria-hidden="true"
ng-class="menuDisplayed ? 'menuon fa-times' : 'menuoff fa-bars'"
ng-click="menuDisplayed = !menuDisplayed">
</i>
在主HTML我加入這樣的指令和範圍值。
<ss-left-nav-directive display-mode="tests"></ss-left-nav-directive>
爲什麼不從顯示模式範圍傳遞測試?我在這裏做錯了什麼?
如果'tests'陣列屬於你的指令,那麼你不需要使用隔離範圍來獲得它。你可以簡單地使用'$ scope.tests'來檢索它。如果屬於父控制器,然後再看@ 32teeth的回答下面 – Ladmerc
該指令控制器和向鏈路功能訪問的指令的**隔離** *孩子*範圍。指令雙向'='綁定屬性訪問*父*範圍變量。有關更多信息,請參閱[AngularJS開發人員指南 - 隔離指令的範圍](https://docs.angularjs.org/guide/directive#isolating-the-scope-of-a-directive) – georgeawg