【发布时间】: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