Angular Js newbie - link in a controller view that triggers another controller action

前端 未结 2 601
失恋的感觉
失恋的感觉 2020-12-12 04:33

angular newbie here.

Let\'s say I have an application that needs to monitor different things like \"news submitted\" \"login attemps\" and so on. My mainController w

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-12 05:01

    You can create a service, that will be storing the data between the controllers. Then you can use / change that piece of data in both of your controllers and associated views.

    app.service('newsDataService', function() {
    
            this.newsData = [];
    
    });
    

    then in your controller,

    app.controller('newsController', function($scope, 'newsDataService') {
    
        // access the data using newsDataService.newsData.
    
    });
    
    
    app.controller('loginAttemptsController', function($scope, 'newsDataService') {
    
        // access the data using newsDataService.newsData.
    
    });
    

    More information on AngularJs Services : https://docs.angularjs.org/guide/services

提交回复
热议问题