简体   繁体   English

如何在 angularJs 1.5 中将 $scope 变量作为组件的 templateUrl 的一部分传递?

[英]How can I pass a $scope variable as part of component's templateUrl in angularJs 1.5?

I have the following code:我有以下代码:

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

myModule.controller('newController', ['$scope', function($scope) {
                   $scope.id = '1234';
}])
.component('myModalX', {
         templateUrl: `./partials/locationY?id=${$scope.id}`,
         controller: 'newController',
         controllerAs: 'vm'
});

This doesn't work.这不起作用。 How can I ensure that I can pass $scope.id as part of the templateUrl?如何确保我可以将$scope.id作为 templateUrl 的一部分传递?

Update: I figured out how to do it.更新:我想出了怎么做。 I created a service that has a setter method that accepts the scope I need as a parameter, and a getter method that accesses the set variable in the service.我创建了一个服务,它有一个接受我需要的范围作为参数的 setter 方法,以及一个访问服务中的 set 变量的 getter 方法。

This way I'm able to access the variable in scope using the getter method in the service.这样我就可以使用服务中的 getter 方法访问范围内的变量。

myModule.controller('newController', ['$scope', 'myScopeAcessingService', function($scope, myScopeAcessingService) {
                   $scope.id = '1234';
                   myScopeAcessingService.setValue($scope);

}])
.component('myModalX', {
         templateUrl: function(myScopeAcessingService) {

           const theIdIneed = myScopeAcessingService.getValue();

           return `./partials/locationY?id=${theIdIneed}`
         },
         controller: 'newController',
         controllerAs: 'vm'
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM