I am building a single page app with Angular JS. The problem is the custom directive mySlider cannot access the scope of the controllers I assign to the template. Is there a way to solve this problem? Here are my files
index.html
<script src="js/dishesapp.js"></script>
<div ng-view></div>
partials/dishestemplate.html
<div my-slider></div>
directives/slider.html
<div id="dishsliderholder">
<ul class="bxslider">
<li class="dishdetails" ng-repeat="dish in dishes">
<div class="item">
<div class="title">{{dish.name}}</div>
<div class="description">{{dish.description}}</div>
</div>
</li>
</ul>
</div>
dishesapp.js
var dishesApp = angular.module('dishesApp', ['ngRoute']);
dishesApp.config(function ($routeProvider) {
$routeProvider
.when('/', {templateUrl: 'partials/default.html'})
.when('/Noodles', {templateUrl: 'partials/dishtemplate.html', controller: 'NoodlesCtrl'})
.when('/Rolls', {templateUrl: 'partials/dishtemplate.html', controller: 'RollsCtrl'})
.when('/Pancakes', {templateUrl: 'partials/dishtemplate.html', controller: 'PancakesCtrl'})
.when('/Rice', {templateUrl: 'partials/dishtemplate.html', controller: 'RiceCtrl'})
.when('/FamilyStyle', {templateUrl: 'partials/dishtemplate.html', controller: 'FamilyStyleCtrl'})
.when('/Others', {templateUrl: 'partials/dishtemplate.html', controller: 'OthersCtrl'})
.otherwise({redirectTo: '/'});
});
dishesApp.controller('NoodlesCtrl', function ($scope, $http) {
$scope.mycategory = "Noodles";
$http.get("http://www.blabla.com/Data/dishes.php").success(function (response) {
$scope.dishes = response.records;
});
});
dishesApp.controller('RollsCtrl', function ($scope, $http) {
$scope.mycategory = "Rolls";
$http.get("http://www.blabla.com/Data/dishes.php").success(function (response) {
$scope.dishes = response.records;
});
});
……………..
dishesApp.directive('mySlider', function() {
return {
restrict: 'A',
templateUrl: 'directives/slider.html',
link: function (scope, element, attrs) {
angular.element( document.querySelector('.bxslider')).bxSlider();
angular.element( document.querySelector('#dishsliderholder')).css("background-size", getSliderHolderBackgroundSize());
}
};
});
ng-app="dishesApp"on your page? Your code is working for me - jsfiddle.net/w5mh927p