Angularjs 如何在范围内保存服务价值?

Angularjs 如何在范围内保存服务价值?,angularjs,Angularjs,嗨, 我尝试在controller中使用服务值JSON并转到另一个视图: .controller('TrainCtrl', function ($scope, $sce, $state, medias) { $scope.train = function(id){ medias.getMedias().then(function(message){ $scope.movie = message.training; // O

嗨, 我尝试在controller中使用服务值JSON并转到另一个视图:

.controller('TrainCtrl', function ($scope, $sce, $state, medias) {

    $scope.train = function(id){
        medias.getMedias().then(function(message){
            $scope.movie = message.training;
            // Object {id: 1, title: ""...}
            console.log($scope.movie);
        });
        // undefined
        console.log($scope.movie);
        // View is empty because $scope.movie is undefined
        $state.go('training');
    };})

    .factory('medias', function($http, $log, $q){
    return{
        getMedias: function(){
            console.log('getMedias');
            var deferred = $q.defer();
            $http.get('js/medias.json')
            .success(function(data){
                deferred.resolve(data);
            })
            .error(function(msg, code){
                deferred.reject(msg);
                $log.error(msg,code)
            });
            return deferred.promise;
        }
    }
});
如何保存服务的结果以在控制器中使用,该控制器在指令中重定向

我的Json:

{"training" :
{"id" : 1,
"url": "http://player.vimeo.com/external/85569724.sd.mp4?s=43df5df0d733011263687d20a47557e4",
"content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim",
"type": "video/mp4"
}
}

它也不起作用:

$scope.train = function(id){
    medias.getMedias().then(function(message){
        //$scope.movie = message.training;
        $scope.movie={
        url: $sce.trustAsResourceUrl("http://player.vimeo.com/external/85569724.sd.mp4?s=43df5df0d733011263687d20a47557e4"),
        content: 'Lorem ipsum dolor sit amet',
        type: 'video/mp4'
    };
        $state.go('training');
    });

};

Thx

让我们来谈谈异步性

$scope.train = function(id){

    medias.getMedias().then(function(message){
        //This is async callback
    });
    // ! This is normal execution, calling movie syncronously, movie is not yet defined !
    console.log($scope.movie);
    $state.go('training');
};})
在http get之后调用.then,可以在程序执行期间随时调用它。为了工作,你应该把所有的东西都放在箱子里


Angular将相应地更新视图。

谢谢,但我这样做了,我的视图仍然是空的。它只有在我编写$scope.movie={id:1,title:aaaa,…}时才有效;直接在$state.go之前。我不明白我的错误在哪里。登录消息时会得到什么?我看到对象{id:1,title:,…},但指令是空的,就像$scope.movie不存在一样。如果我添加$scope.movie={…}$state.go;在getMedias函数之后,它当然可以工作。我想说的是message.training而不是message,您确定这里的培训不太多吗?我的JSON文件包含{training:{id:..,title…}。我试图直接添加到代码中:$scope.movie={url:$sce.trustAsResourceUrl,内容:'Lorem ipsum dolor',键入:'video/mp4'};但它也不起作用。
$scope.train = function(id){
    medias.getMedias().then(function(message){
        $scope.movie = message.training;
        // Object {id: 1, title: ""...}
        console.log($scope.movie);
        // undefined
        console.log($scope.movie);
        // View is empty because $scope.movie is undefined
        $state.go('training');
    });
};})