无法从 Javascript 设置 Angular 模型



我试图从Javascript代码中设置一个angularjs模型值,但它不起作用。我总是得到该属性的空值。下面是我的 html 代码片段:

  <script type="text/javascript">
    VSS.init({
        explicitNotifyLoaded: true,
        usePlatformScripts: true,
        usePlatformStyles: true
    });
    VSS.ready(function () {            
        console.log("Organization name " + VSS.getWebContext().account.name);
        var scope = angular.element(document.querySelector('#hubContainer')).scope();
        scope.$apply(function () {
            scope.DashboardModel.AccountKey = VSS.getWebContext().account.name;
        })
    });
   </script>
  <div class="ng-cloak tree-master-wrapper">
    <div id="hubContainer" class="ng-scope" data-ng-controller="MyController">
       Some code comes here…….
   </div>

但由于某些原因,控制台.log("帐户密钥" + $scope。DashboardModel.AccountKey( 为空。知道吗?我使用 https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js

我能够通过将此条目添加到"MyController"来解决此问题:

MyModule.controller('MyController', ['$scope', function($scope){
    $scope.DashboardModel = new DashboardModel(); 
    var checkPermission = function (selectedTeam) {
    VSS.require(["VSS/Service", "VSS/Security/RestClient"], 
    function(VSS_Service, Security_RestClient) {   
       $scope.DashboardModel.AccountKey = VSS.getWebContext().account.name;
    });
   });
  }

最新更新