Javascript Angularjs:获取在指令内部的另一个控制器中定义的控制器中范围变量的值
我有一个定义控制器的指令,其中有一个变量,比如“Javascript Angularjs:获取在指令内部的另一个控制器中定义的控制器中范围变量的值,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我有一个定义控制器的指令,其中有一个变量,比如“$scope.accesJson”。我需要从另一个控制器访问它 代码: angular.module('test.directives').directive("manageAccess", function() { return { restrict: "E", replace: true, templateUrl: "template/test.html", control
$scope.accesJson
”。我需要从另一个控制器访问它
代码:
angular.module('test.directives').directive("manageAccess", function() {
return {
restrict: "E",
replace: true,
templateUrl: "template/test.html",
controller: function($scope, $element, $http) {
$scope.accesJson = ["hi","hello"];
}
};
});
我有另一个控制器
angular.module("test.controllers").controller("testController", function($scope, $http) {
$scope.getUsers = function() {
console.log $scope.accesJson //I need value of $scope.accesJson here.
}
});
我该怎么做
请帮忙,
感谢在两个控制器之间共享资源,您可以始终使用服务或工厂。您也可以通过定义一个全局变量来实现,但不鼓励这样做 申报工厂:
var app = angular.module('app',[])
.factory('appSvc',[function(){
var resources = {};
return resources;
}]);
请注意,您可以在工厂内声明可重用函数
声明工厂后,请记住将其正确注入需要它的控制器中
app.controller('appCtrl',['appSvc',function(appSvc){
//do something with your appSvc
}]);
下面是一个非常简单的示例,演示如何使用service/factory获取和设置数据
有关详细文档:要在两个控制器之间共享资源,您可以始终使用服务或工厂。您也可以通过定义一个全局变量来实现,但不鼓励这样做 申报工厂:
var app = angular.module('app',[])
.factory('appSvc',[function(){
var resources = {};
return resources;
}]);
请注意,您可以在工厂内声明可重用函数
声明工厂后,请记住将其正确注入需要它的控制器中
app.controller('appCtrl',['appSvc',function(appSvc){
//do something with your appSvc
}]);
下面是一个非常简单的示例,演示如何使用service/factory获取和设置数据
对于深入的文档:用于在不同控制器服务之间共享数据是一个不错的选择。这样定义一个人
angular.module("test.services").factory('DataBasket', function () {
return {};
});
在指令中
controller: function($scope, $element, $http, DataBasket) {
DataBasket.accessJson = ["hi", "hello"];
$scope.accesJson = DataBasket.accessJson;
}
来自其他控制器
angular.module("test.controllers").controller("testController", function($scope, $http, DataBasket) {
$scope.getUsers = function() {
console.log DataBasket.accesJson
}
});
对于在不同控制器之间共享数据,服务是一个很好的选择。这样定义一个人
angular.module("test.services").factory('DataBasket', function () {
return {};
});
在指令中
controller: function($scope, $element, $http, DataBasket) {
DataBasket.accessJson = ["hi", "hello"];
$scope.accesJson = DataBasket.accessJson;
}
来自其他控制器
angular.module("test.controllers").controller("testController", function($scope, $http, DataBasket) {
$scope.getUsers = function() {
console.log DataBasket.accesJson
}
});
您还可以在指令的链接函数中将外部$scope的属性绑定到指令,如下所示:
angular.module('foo', [])
.directive("manageAccess",
function() {
return {
restrict: "E",
replace: true,
scope: {
property: '='
},
link: function($scope) {
$scope.property = { foo: 1 }
}
}
}
)
.controller('Main',
function($scope) {
$scope.showAccessJsonValue = function() {
$scope.value = $scope.accessJson
}
}
)
然后在模板中,您可以单击ng
,调用showAccessJsonValue
,这将为您提供在指令中指定的值
像
显示价值
{{value}json}
下面是一个。您还可以在指令的链接函数中将外部$scope的属性绑定到指令,如下所示:
angular.module('foo', [])
.directive("manageAccess",
function() {
return {
restrict: "E",
replace: true,
scope: {
property: '='
},
link: function($scope) {
$scope.property = { foo: 1 }
}
}
}
)
.controller('Main',
function($scope) {
$scope.showAccessJsonValue = function() {
$scope.value = $scope.accessJson
}
}
)
然后在模板中,您可以单击ng
,调用showAccessJsonValue
,这将为您提供在指令中指定的值
像
显示价值
{{value}json}
这是一个例子