【问题标题】:How can I pass a $scope variable as part of component's templateUrl in angularJs 1.5?如何在 angularJs 1.5 中将 $scope 变量作为组件的 templateUrl 的一部分传递?
【发布时间】:2020-01-16 18:36:04
【问题描述】:

我有以下代码:

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'
});

这不起作用。如何确保我可以将 $scope.id 作为 templateUrl 的一部分传递?

【问题讨论】:

标签: javascript angularjs routes angularjs-scope


【解决方案1】:

更新:我想出了如何去做。 我创建了一个服务,它有一个接受我需要的范围作为参数的 setter 方法,以及一个访问服务中的 set 变量的 getter 方法。

通过这种方式,我可以使用服务中的 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'
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-28
    • 2017-08-20
    • 1970-01-01
    • 2017-07-23
    • 2014-07-20
    • 1970-01-01
    • 2016-05-26
    • 2019-01-19
    相关资源
    最近更新 更多