I'm new in AngularJS, and I have some trouble to update values in Service, Here is my code:
JS
(function() {
'use strict';
function TestController($scope,$element){
}
function InfoController($scope,$element,$http,testService){
$scope.$watch(function () { return testService.getHandler(); },
function (value) {
$scope.sel = value;
var sel = $scope.sel;
$http.get('hanlder',{params: {sel:sel}}).success(function(resp) {
$scope.data = resp['data'];
});
}
);
}
function OptionController($scope,$element,$http,testService){
var list = $element;
list.on('click',".list-group-item",function() {
var sel = $(this).text();
testService.setHandler(sel);
});
}
function testService() {
var property = "List";
return {
setHandler: function (msg) {
console.log(msg); //After click, property == msg
property = msg;
},
getHandler: function () {
console.log(property); //But property in getHandler doesn't change // != msg
return property;
}
};
}
angular.module('testModule', [])
.service('testService', testService)
.controller('TestController', TestController)
.controller('InfoController', InfoController)
.controller('OptionController', OptionController);
})();
After click ".list-group-item" in OptionController, new value sel will be send to testService.setHandler (==msg), and change value property in testService, so getHanler will return new value and InfoController will have new value sel.
But, in real app, property in getHanlder doesn't change, and InfoController can't get new value sel to run $http.get(). Please, help me to fix that:)
Here my html:
<section ng-app = "testModule" ng-cotroller="TestController" class="main-content">
<div class="row ">
<div class="col-md-6 col-md-offset-1 list-group" ng-controller="InfoController">
<div class="list-group-item list-group-item-info" ng-show="sel">{{sel}}
</div>
<!--Before click-->
<div class="list-group-item" ng-if="sel == 'List'">Choose one in right!</div>
<!--After click-->
<div class="list-group-item sel-dat" ng-repeat="dat in data track by $index" ng-if="sel != 'List'">
<h4>Name: {{dat.name}}</h4>
</div>
</div>
<div class="col-md-3 col-md-offset-1 list-group aa" ng-controller="OptionController">
<div class="list-group-item">Users</div>
<div class="list-group-item">Kinds</div>
</div>
</div>
</section>