如何将范围变量从我的控制器传递给javascript

时间:2015-07-18 14:44:44

标签: javascript angularjs

嗨,我有一个控制器变量

         $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传递给脚本标记?

1 个答案:

答案 0 :(得分:0)

是的,有另一种方法可以使用$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>