【问题标题】:AngularJS variable from controller to directive从控制器到指令的 AngularJS 变量
【发布时间】:2016-07-30 19:34:38
【问题描述】:

我想问你是否有可能,如果有,那么我如何将一些变量从控制器传递到指令。 这是我的一些代码:

app.js

var VirtualGallery = angular.module('virtualGallery', ['VirtualGalleryControllers', 'ngRoute']);

VirtualGallery.constant('apiURL', 'roomPicture');

VirtualGallery.run(['$rootScope', function ($rootScope) {
    $rootScope.roomPictures = [];
}]);

var VirtualGalleryControllers = angular.module('VirtualGalleryControllers', ['ngRoute']);

VirtualGalleryControllers.controller('AppCtrl', function ($http, $rootScope, $scope, apiURL, $q) {
$scope.getallrooms = function () {
    $http.get(apiURL)
        .success(function (data) {
            $rootScope.roomPictures = data; //idk whether to use $scope or $rootScope
        });
    };
});

在这个 app.js 中,我试图从 DB 中获取一些数据,这些数据需要在指令中使用。

指令

angular.module('virtualGallery')
    .directive('ngWebgl', function () {
    return {
        restrict: 'A',
        scope: {
            'getallrooms': '=',
            'roomPictures': '='
        },
        link: function postLink(scope, element, attrs) {
            scope.init = function () {
                //here I would like to be able to access $scope or $rootScope from app.js file.
             };
          }
       };
    });

在指令中,我需要在函数 init() 中访问 $scope 或 $rootScope,我需要在其中使用该数据。

HTML

 <body ng-app="virtualGallery">
 <div class="container" ng-controller="AppCtrl">

<div
    id="webglContainer"
    ng-webgl
    getallrooms="getallrooms"
    roomPictures="roomPictures"
    ></div>
  <p ng-model="roomPictures"></p>
  <p ng-model="getallrooms"></p>
</div>
<script type="text/javascript" src="js/vg.js"></script>
<script type="text/javascript" src="js/ngWebgl.js"></script>

在 html 中,我试图将该数据从 app.js 传递给指令。

我对 Angular 很陌生,这甚至是我的第一个指令,所以我有点困惑。每一个帮助将不胜感激。谢谢大家:)

【问题讨论】:

    标签: javascript html angularjs


    【解决方案1】:

    在你的 app.js 中使用这样的控制器

    VirtualGalleryControllers.controller('AppCtrl', function ($http, $rootScope, $scope, apiURL, $q) {
    $scope.getallrooms = function () {
        $http.get(apiURL)
            .success(function (data) {
                $scope.roomPictures = data; //use $scope instead of  $rootScope
            });
        };
    });
    

    然后为你的指令:

    angular.module('virtualGallery')
    .directive('ngWebgl', function () {
      return {
        restrict: 'A',
        scope: {
            pictures: '=virtualGallery'
        },
        link: function postLink(scope, element, attrs) {
            scope.init = function () {
                // you can access the variable through the scope
                scope.pictures;
             };
        }
      };
    });
    

    或者您可以简单地在指令中发出 http 请求并在那里操作数据。

    【讨论】:

    • 谢谢,我在指令中移动了 http 请求。它奏效了。我希望它能做我想做的事情:)
    • 我还有一个问题。现在,当我通过 http 请求获取数据时,如何将这些数据传递给我的 html.file ?(模板)
    • 只需将数据添加到作用域中,并以双花括号中的角度变量或使用 ng-bind 访问它
    【解决方案2】:

    您可以将 $rootScope 注入您的指令(就像您在控制器中所做的那样),然后访问该 rootScope 变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      • 2016-01-11
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多