尝试观看数组(' CurrentDetails')。但更新后,监视功能内的数据为空。我在哪里错了。
myctrl.js:
app.controller("myctrl", function ($scope, SharedService) {
$scope.CurrentDetails = [];
var init = function(){
var data = {
OptionsType: "LanguageOptions",
OptionItems: [{ "OptionId": 1, "OptionName": "English"}],
Breadcrumbs: ["Home"]
}
$scope.ShowResponse(data);
}
$scope.ShowResponse = function (data) {
$scope.CurrentDetails = data;
console.log($scope.CurrentDetails); //can see the data here
}
$scope.$watch('CurrentDetails', function () {
console.log($scope.CurrentDetails); //no data here
SharedService.updateCurrentDetails($scope.CurrentDetails);
},true);
$scope.$on('valuesUpdated', function () {
$scope.CurrentDetails = SharedService.CurrentDetails;
});
init();
}
sharedservice.js:
app.service("SharedService", ['$rootScope', function ($rootScope) {
var service = {};
service.CurrentDetails = [];
service.updateCurrentDetails = function (value) {
this.CurrentDetails = value;
$rootScope.$broadcast("valuesUpdated");
}
}]);