我在學習第一個Angular項目時遇到了問題。Angular.js在ng-include html模板中沒有作用域
目標:當單擊放置在一個給定的.html模板的鏈接NG-包括,我希望它改變$ scope.selectedLocation
問題的價值:的價值$ scope.selectedLocation不會改變。
我讀ng-include創建一個子作用域,所以爲了更改父作用域變量,可以將$ parent放在值的前面。我已經嘗試過,並且不起作用。
主索引頁:
<body ng-app="photoApp" id="bodyDiv" >
<div ng-controller="PhotoGallery">
<div>
<ng-switch on="selectedLocation" >
<div ng-switch-when="home" >
<div ng-include="'home.html'"></div>
</div>
<div ng-switch-when="loc1">
<div ng-include="'file1.html'"></div>
</div>
<div ng-switch-when="loc2">
<div ng-include="'file2.html'"></div>
</div>
</ng-switch>
</div>
</div>
</body>
home.html的代碼:
<div class="container-fluid">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6">
<a href="#" ng-click="selectedLocation='loc1'">
Location 1
</a>
</div>
<div class="col-lg-6 col-md-6 col-sm-6">
<a href="#" ng-click="selectedLocation='loc2'">
Location 2
</a>
</div>
</div>
</div>
photoApp.js代碼:
var photoApp= angular.module('photoApp', []);
westonPhotographyApp.controller('PhotoGallery', function($scope)
{
$scope.selectedLocation ="home";
}
使用 '控制器'語法有竅門!我從中學到了很多,謝謝! –