小编Cha*_*cho的帖子

AngularJS模块/范围共享

我最近开始使用AngularJS,现在我正在构建我的应用程序的方式是这样的:

MainController.js

var app = angular.module('app', ['SomeController', 'MainController']);

app.controller('MainController', function ($scope) {
    // do some stuff
}
Run Code Online (Sandbox Code Playgroud)

SomeController.js

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

SomeController.controller('SomeController', function ($scope) {
    $scope.variable = "test";
    // do some otherstuff
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是模块之间没有共享范围.从MainController我不能得到变量"test"例如.

  • 这是什么最好的做法?我是否将1个模块中的所有控制器存储在1个文件中?
  • 我怎么能有1个页面有2个控制器并$scope在它们之间共享,或者可以将所有内容放在一个控制器中?

angularjs angularjs-scope angularjs-module

9
推荐指数
1
解决办法
2万
查看次数

在多个类之间共享变量

如果我有3个类,可以说:Mainclass,ChildClass,OtherChild.

MainClass()
{
     ChildClass cc = new ChildClass();
     OtherChild oc = new OtherChild();

     //Set the name property of childclass
     string childName = "some name";
}

ChildClass()
{
    public string name {get; set;}
}

OtherChild()
{
     //Here i want to get the name property from ChildClass()
     //Doing this will make a new instance of ChildClass,  which will not have the name property set.
     ChildClass cc = new ChildClass(); 

}
Run Code Online (Sandbox Code Playgroud)

这是什么解决方案?

.net c#

3
推荐指数
1
解决办法
4万
查看次数

标签 统计

.net ×1

angularjs ×1

angularjs-module ×1

angularjs-scope ×1

c# ×1