访问对象内的对象 - Javascript/JQuery

问题描述 投票:0回答:1

我如何访问 testController 中的 myUrl 对象,我已定义如下指令

paginationModule.directive('myDirective', function() {
  return {
    restrict: 'E',
   
    scope: {
      'myUrl': '=',       // binding strategy
      'myLinkText': '='   // binding strategy
    },
     controller: testController,
            controllerAs: 'testCtrl',
            bindToController: true,
    template: gridPaginationTemplate
  };
});

从 html 中我传递了一些对象,如下所示

<my-directive
     my-url="paysCtrl.gridOptions"
     my-link-text="paysCtrl.gridApi"></my-directive> 

enter image description here

我已经尝试过以下选项,但所有选项都未定义

console.info($scope['testCtrl']['myUrl']); console.info($scope['testCtrl'].myLinkText);

angular angularjs
1个回答
0
投票

您可以直接通过

$scope
访问它,如
$scope.myUrl

angular.module('components', []).directive('helloWorld', function() {
  return {
    restrict: 'E',
    scope: {
      name: '='
    },
    template: '<span>Hello {{name}}</span>',
    controller: ['$scope', function MyTabsController($scope) {
      console.log($scope.name);
    }]
  }
})

angular.module('myApp', ['components']).controller('myctrl', ['$scope', function($scope) {
  $scope.name = 'test';

}]);
.ng-scope{
    border: 1px red solid;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.8.3/angular.min.js"></script>
<div ng-app="myApp" ng-controller='myctrl'>
    <hello-world name="name"></hello-world>
<div>

© www.soinside.com 2019 - 2024. All rights reserved.