使用其他服务在同一控制器中的函数之间传递数据以获取数据

时间:2017-07-22 22:44:26

标签: javascript angularjs controller

我从另一个Controller通过Myservice获取一些数据。我可以从视图中看到import UIKit class RadioWaveAnimationView: UIView { var animatableLayer : CAShapeLayer? override func awakeFromNib() { super.awakeFromNib() self.layer.cornerRadius = self.bounds.height/2 self.animatableLayer = CAShapeLayer() self.animatableLayer?.fillColor = self.backgroundColor?.cgColor self.animatableLayer?.path = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.layer.cornerRadius).cgPath self.animatableLayer?.frame = self.bounds self.animatableLayer?.cornerRadius = self.bounds.height/2 self.animatableLayer?.masksToBounds = true self.layer.addSublayer(self.animatableLayer!) self.startAnimation() } func startAnimation() { let layerAnimation = CABasicAnimation(keyPath: "transform.scale") layerAnimation.fromValue = 1 layerAnimation.toValue = 3 layerAnimation.isAdditive = false let layerAnimation2 = CABasicAnimation(keyPath: "opacity") layerAnimation2.fromValue = 1 layerAnimation2.toValue = 0 layerAnimation2.isAdditive = false let groupAnimation = CAAnimationGroup() groupAnimation.animations = [layerAnimation,layerAnimation2] groupAnimation.duration = CFTimeInterval(2) groupAnimation.fillMode = kCAFillModeForwards groupAnimation.isRemovedOnCompletion = true groupAnimation.repeatCount = .infinity self.animatableLayer?.add(groupAnimation, forKey: "growingAnimation") } /* // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func draw(_ rect: CGRect) { // Drawing code } */ } ,但{{users.data}}users.length = 0为空,这意味着我无法访问getData函数中的MyService内容。如果我将MyService替换为json的数据就像 $data它运作正常 谢谢..

$scope.users=[{..},{..}]

1 个答案:

答案 0 :(得分:0)

当您在第一个控制器中获取数据时,您调用MyService.setData(data);该服务将存储在其本地var data中并保留在那里。然后在第二个控制器中,您可以通过调用MyService.getData()

来检索该数据
app.service('MyService', function() {

    var ret =  {
        data: [],
        setData: function(inData) {
            ret.data = inData;
        },
        getData: function() {
            return ret.data;
        }
    };
    return ret;
});

第一个控制器1

app.controller('EventCtrl', ['$scope', 'EventService', 'MyService',

    function ($scope, EventService , MyService) {

        var baseUrl = '';

        $scope.getEvents=function()
        {
            var apiRoute = 'http://localhost:9811/notification/notification/';
            var _Event = EventService.getAll(apiRoute);
            _Event.then(function (response) {
                    $scope.events= response.data;
                    MyService.data = $scope.events;
                    MyService.setData($scope.events);
                    $scope.VarCtrl1= MyService;

                },
                function (error) {
                    console.log("Error: " + error);
                });

        }
        $scope.getEvents()
    }]); 

我更新了服务,但它不起作用..所以我修改了第一个这样的控制器你觉得怎么样?

app.controller('EventCtrl', ['$scope', 'EventService', 'MyService',

  function ($scope, EventService , MyService) {

    var baseUrl = '';

    $scope.getEvents=function()
    {
        var apiRoute = 
   'http://localhost:9811/notification/notification/';
        var _Event = EventService.getAll(apiRoute);
        _Event.then(function (response) {
            var data = response.data
                MyService.setData(data);
                $scope.VarCtrl1= MyService;

            },
            function (error) {
                console.log("Error: " + error);
            });

    }
    $scope.getEvents()
}]); 

感谢服务的更新现在它更好...我可以在$ users和$ data in tableController中有数据但是orderBy需要一个数组但我得到这个:(当我做consoleLoge($ scope.users)        Object {data:Array(0),setData:function,getData:function} data:        Array(9)0:Object1:Object2:Object3:Object4:Object5:Object6:        Object7:Object8:Objectlength:9__proto __:Array(0)getData:        function()setData:function(inData) proto :Object        tableController.js:24