如何在角度js中对状态改变事件注入$ http服务?

时间:2016-09-27 05:38:24

标签: angularjs angular-ui-router

我在角度js中使用Ui-Route,我希望在每次状态更改时调用def setFutureIndex = { val IndexStoreDir = Paths.get("/var/www/html/LuceneIndex") val analyzer = new StandardAnalyzer() val writerConfig = new IndexWriterConfig(analyzer) writerConfig.setOpenMode(OpenMode.CREATE) writerConfig.setRAMBufferSizeMB(500) val directory = FSDirectory.open(IndexStoreDir) val writer = new IndexWriter(directory, writerConfig) val notes = notesService.getNotes() //Gets all notes from slick. Data is coming in getNotes() var doc = new Document() def indexingFuture = { val list = Seq ( notes.map(_.foreach { case (note) => writeToDoc(note, writer) }) ) Future.sequence(list) } Await.result(indexingFuture, Duration.Inf) /*indexingFuture.onComplete { case Success(value) => println(value) case Failure(e) => e.printStackTrace() }*/ } def writeToDoc(note: NoteEntity, writer: IndexWriter) = Future { println("*****Indexing: " + note.id.get) var doc = new Document() var field = new TextField("title", " {##" + note.id.get + "##} " + note.title, Field.Store.YES) doc.add(field) field = new TextField("teaser", note.teaser, Field.Store.YES) doc.add(field) field = new TextField("description", note.description, Field.Store.YES) doc.add(field) writer.addDocument(doc) writer.commit() println("*****Completed: " + note.id.get) var status = "*****Completed: " + note.id.get } 服务。当我在statechange事件中注入$http服务时,它会给出循环依赖的错误,甚至我在拦截器中注入同样的错误。请帮帮我。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以将服务调用解析为状态

$stateProvider
    .state('business-page', {
        url: '/your-listing/:id',
        controller: 'BusinessCtrl as vm',
        templateUrl: 'pages/business/business.html',
        title: 'Details',
        resolve: {
            listing: ['appDataFactory', '$stateParams', function (appDataFactory, $stateParams) {
                return appDataFactory.getListingData($stateParams.id);
            }]
        }
    })

其中appDataFactory将是$ http请求的服务,响应可以作为状态控制器上的注入来解决