javascript - Cannot set a value in $scope in Angular? -
i following tutorial on lynda.com angularjs essential training.
part of index file looks like:
<div class="container" ng-controller="appctrl"> <h1>angulair</h1> <ul class="nav nav-pills"> <li role="presentation" ng-class="destinationsactive"><a href="#/" ng-click="setactive(destinations)">destinations</a></li> <li role="presentation" ng-class="flightsactive"><a href="#/flights" ng-click="setactive(flights)">flights</a></li> <li role="presentation" ng-class="reservationsactive"><a href="#/reservations" ng-click="setactive(reservations)">reservations</a></li> </ul> <div ng-view> </div> <p>{{ flightsactive }}</p> </div>
now when click on link should fire setactive function defined in appctrl looks this:
$scope.setactive = function (type) { $scope.destinationsactive = ''; $scope.flightsactive = ''; $scope.reservationsactive = ''; $scope[type + 'active'] = 'active'; };
now problem simple. function should take type example 'destinations' , append 'active' , set scope variable 'destinationsactive' active in turn should reflected in ng-class directive of li tags , link should active.
i have tried insert alert('hello');
after setting active fires up. means function indeed being called. when alert($scope.destinationsactive);
gives me blank alert whereas should give me active value.
i not following exercise files , feel maybe because tutorial relatively older, there might changes in framework. have encountered such problems tutorial. anyway, doing wrong?
in ng-click
directives passing argument variable, not string.
ng-click="setactive(destinations)"
will pass in value of $scope.destinations
, undefined. try passing in string i.e.:
ng-click="setactive('destinations')"
note single quotes
Comments
Post a Comment