如何通过范围变量从我的控制器到javascript

How to pass scope variables from my controller to javascript

本文关键字:控制器 javascript 我的 何通过 范围 变量      更新时间:2023-09-26

嗨,我有一个控制器变量

         $scope.submitQuestion = function(){
         $scope.post.createdTime = createdtime;
         $scope.post.creator = $scope.user.profile.username;
         $scope.post.creatorUID = $scope.user.uid;
           $scope.post.creatorpic = $scope.user.profile.userpic;
        $scope.post.postmedia = $scope.object.video.info.uuid;
        $scope.post.postmediaimage = $scope.object.image.info.uuid;
       Post.create($scope.post).then  (function(ref) {
    $location.path('/posts/' + ref.name());
     });
     };

这是我的视图,我打算使用

         <body>
       <div id="player"></div>
         <script>
          var player = new Clappr.Player({source: 
             {{post.postmedia}} , parentId: "#player"});
        </script>
        </body>

是否可以传递我的作用域变量$scope.post。Postmedia到script标签?

是的,还有另一种使用$window服务的方式。它等于javascript的window对象。你应该首先分配一个 attribute to window object ,并在你的多个js文件的任何地方使用这个全局变量值。

您应该首先在控制器中注入$window服务。你的控制器声明看起来像

angular.controller('MyController', ['$scope', '$window', function ($scope, $window){
    $window.post.postmedia = $scope.object.video.info.uuid; // Assign value first
}])

现在你可以在任何地方使用这个属性值。像

<script>
     var player = new Clappr.Player({source: 
     post.postmedia , parentId: "#player"}); //post.postmedia will work if it will have been assigned first else it will show undefined
</script>