如何使用$rootScope我想稍后在另一个控制器中访问的控制器中存储变量?例如:
angular.module('myApp').controller('myCtrl', function($scope) {
var a = //something in the scope
//put it in the root scope
});
angular.module('myApp').controller('myCtrl2', function($scope) {
var b = //get var a from root scope somehow
//use var b
});
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
我有这个:
app.controller('foo1', function ($scope) {
$scope.bar = 'foo';
});
app.controller('foo2', function ($scope) {
// want to access the $scope of foo1 here, to access bar
});
Run Code Online (Sandbox Code Playgroud)
我怎么做到这一点?
我的角应用程序有2个控制器.我的问题是,当用户离开页面时,控制器不会保留数据.
如何将控制器上的选定数据存储到数据存储中,以便在其他控制器之间使用?
在上周,我一直试图理解双向数据绑定(Angular)和单向数据流(React/Flux)是如何不同的.他们说单向数据流更强大,更容易理解和遵循:它是确定性的,有助于避免副作用.然而,在我的新手眼中,它们看起来几乎相同:视图监听模型,模型对视图所做的操作做出反应.两者都声称模型是真实的唯一来源.
任何人都能以可理解的方式全面解释它们之间的真正差异以及单向数据流如何更有益且更容易推理?
是否可以将控制器注入另一个属于同一模块的控制器?
例:
var app = angular.module('myAppModule', [])
.controller('controllerOne', ['$scope', function($scope){
$scope.helloWorld = function(){
return 'Hello World';
}
}])
.controller('controllerTwo', ['$scope', 'controllerOne', function($scope, controllerOne){
console.log(controllerOne.helloWorld());
}])Run Code Online (Sandbox Code Playgroud)
我一直在controllerOne上得到未知的提供者.我不知道它是如何可能的,因为它们在同一个模块中共存.任何帮助将非常感激.
javascript dependency-injection angularjs angularjs-scope angularjs-controller
我正在使用angular-ui ui-router作为网络应用程序.我有这样的状态/视图配置:
...
.state('parentState', {
url:'/:id',
views: {
'main@parent': {
controller: 'ParentMainCtrl',
},
'sub@parent': {
controller: 'ParentSubCtrl',
},
},
})
...
Run Code Online (Sandbox Code Playgroud)
现在,我需要在两个州之间共享数据,main并且sub.一种方法是添加一个resolveto parentState并将依赖项注入视图的控制器,但之后我将无法在两个视图之间进行数据绑定.我尝试添加一个data属性parentState,但似乎子视图不继承它.在这种状态下,兄弟视图之间进行双向数据绑定的方法是什么?
我有两个控制器 - searchBoxController和productList.我想要做的是从多个控制器更新范围变量$ scope.products.我知道将它定义为根变量是一个非常糟糕的设计 - 但将其放在共享服务中并不能解决问题.更新未反映在HTML模板中!
function SearchTermService(){
this.productSearch = function(data, $http){
var url = "";
$http.get(url).then(function(resp){
return resp.data;
},
function(err){
console.log(err);
});
};
};
var app = angular.module('app', []);
app.service("myService", MyService);
app.service("searchTermService", SearchTermService);
app.run(function($rootScope) {
$rootScope.products = new Date();
});
app.controller('productList', function ($scope, $rootScope, $http, myService) {
$rootScope.products = prod_res;
});
app.controller('searchBoxController', function($scope, $http, searchTermService, $rootScope){
$scope.getSearchResults = function(){
$rootScope.products = searchTermService.productSearch($scope.term, $http)
};
});
Run Code Online (Sandbox Code Playgroud)
PS:我不确定在'searchBoxController'中分配$ rootScope.products时是否需要返回一个promise,因为console.log表示未定义.目前我没有从服务中退回承诺.
我有两个控制器,其中一个我声明了一个$ scope变量,我希望在第二个控制器中可见.
第一控制器
app.controller('Ctrl1', function ($scope) {
$scope.variable1 = "One";
});
Run Code Online (Sandbox Code Playgroud)
第二控制器
app.controller('Ctrl2', function ($scope, share) {
console.log("shared variable " + share.getVariable());
});
Run Code Online (Sandbox Code Playgroud)
我研究了最好的Angular方法,我发现这是使用服务.所以我添加了一项服务Ctrl1
服务
.service('share', function ($scope) {
return {
getVariable: function () {
return $scope.variable1;
}
};
});
Run Code Online (Sandbox Code Playgroud)
此代码返回此错误:
Unknown provider: $scopeProvider <- $scope <- share
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:可能在控制器之间共享$ scope变量吗?不是最好的角度解决方案或者我错过了什么?
对不起我的琐碎问题,但我是一名Angular初学者.
提前致谢
问候
我有一个用户需要填写并提交的表格。单击“提交”按钮后,触发了一个应将数据推入数组的函数,但是我没有运气将其推入数组。需要将数据推入的阵列在另一个控制器中。
调试代码后,我发现此错误。
Cannot read property 'comments' of undefined
at n.$scope.submitComment (app.js:167)
Run Code Online (Sandbox Code Playgroud)
JavaScript / HTML
Cannot read property 'comments' of undefined
at n.$scope.submitComment (app.js:167)
Run Code Online (Sandbox Code Playgroud)
.controller('DishDetailController', ['$scope', function($scope) {
var dish = {
name: 'Uthapizza',
image: 'images/uthapizza.png',
category: 'mains',
label: 'Hot',
price: '4.99',
description: 'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
comments: [{
rating: 5,
comment: "Imagine all the eatables, living in conFusion!",
author: …Run Code Online (Sandbox Code Playgroud)我有以下 html(示例)
<div ng-controller='ctrl-1'>
<form>
<input type='text' ng-model='name'/>
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
和 js 文件为:
app.controller('ctrl-1',function($scope){
$scope.name = 'john';
});
app.controller('ctrl-2',function($scope){
/*
Here I want to change value of 'name' field which is in 'ctrl-1'
*/
});
Run Code Online (Sandbox Code Playgroud)
如何在 angular js 中实现这一点?
angularjs ×10
javascript ×6
angular-ui ×1
arrays ×1
flux ×1
reactjs ×1
reactjs-flux ×1
rootscope ×1