Javascript 更新angularjs服务和控制器中的数据

Javascript 更新angularjs服务和控制器中的数据,javascript,angularjs,angularjs-service,angularjs-routing,Javascript,Angularjs,Angularjs Service,Angularjs Routing,我有一个叫做“记事本服务”的角度服务: app.factory('NoteBooksService', function() { var permanentStorage = window.localStorage; // Initializing the local storage if no data exists in the local storage. if (permanentStorage.getItem("noteBooks") === null) {

我有一个叫做“记事本服务”的角度服务:

app.factory('NoteBooksService', function() {

    var permanentStorage = window.localStorage;
    // Initializing the local storage if no data exists in the local storage.
    if (permanentStorage.getItem("noteBooks") === null) {
        permanentStorage["noteBooks"] = JSON.stringify([
            {
                name:"Your first notebook", //the defualt title of the notebook
                icon:0, // the defualt icon of the nptebook
                color:"Blue", // the default color of the notebook.
                pages:[ // adding one test page to the notebook.
                    {
                        name:"Your first page",
                        content:"The content of your first page ..."
                    }
                ]
            }
        ]);
    }

    var noteBooks = JSON.parse(permanentStorage["noteBooks"]); // loading the notebooks from local storage.
    return { // providing access to this service
        noteBooks: noteBooks, // used to get all the notebooks
        getNoteBook : function(index) { // returns one particular notebook by index.
            return noteBooks[index]
        }
    }
});
它检查本地存储是否包含密钥,如果不包含,则将密钥添加到本地存储。如果本地存储器具有密钥,它将从本地存储器加载数据

我想在本地存储器中的数据更改时更新服务,还想更新控制器中由该服务初始化的变量

到目前为止,我一直使用
$window.location.reload()每次修改本地存储后,重新呈现整个页面,但现在我需要做两件事

  • 重新呈现整个页面
  • 导航到另一个状态
  • 我尝试了下面的代码,但没有如我预期的那样工作:

    $state.go('NoteBook');
    $window.location.reload();
    

    有没有更好的办法

    好的,我只需要这样做:

    $rootScope.$apply();
    

    如果导航到另一个状态,它会自动加载该新状态的Ctrl和templates?你面临的问题是什么?它不能解决这个问题。它将旧数据保存在服务中。