Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 获取角度为'的$scope对象;s run()方法_Angularjs - Fatal编程技术网

Angularjs 获取角度为'的$scope对象;s run()方法

Angularjs 获取角度为'的$scope对象;s run()方法,angularjs,Angularjs,我想在我的应用程序加载时做一些事情来设置默认状态。所以我尝试在模块对象上使用run方法。当我试图访问$scope变量时,尽管我在控制台中收到一条“uncaughtreferenceerror:$scope未定义”消息 请参见下面的示例 我这样做完全错了吗 例如,我想在开始时运行一次watchAction函数,以隐藏尚未调用的UI元素,但watchAction函数没有$scope对象,因为watch方法没有调用它,所以我必须将其传递给它,但遗憾的是它不可用 app.run(function ($r

我想在我的应用程序加载时做一些事情来设置默认状态。所以我尝试在模块对象上使用run方法。当我试图访问$scope变量时,尽管我在控制台中收到一条“uncaughtreferenceerror:$scope未定义”消息

请参见下面的示例

我这样做完全错了吗

例如,我想在开始时运行一次watchAction函数,以隐藏尚未调用的UI元素,但watchAction函数没有$scope对象,因为watch方法没有调用它,所以我必须将其传递给它,但遗憾的是它不可用

app.run(function ($rootScope) {
    $rootScope.someData = {message: "hello"};
});
您只能将
$rootScope
注入到
服务
运行
函数中,因为每个
子作用域
都是从其父作用域继承而来的,顶级作用域是
rootScope
。因为注入任何作用域都是模棱两可的。只提供根作用域

var app = angular.module('myApp', []);
app.run(function ($rootScope) {
    // use .run to access $rootScope
    $rootScope.rootProperty = 'root scope';
});

app.controller("ParentCtrl", ParentCtrlFunction);
app.controller("ChildCtrl", ChildCtrlFunction);
function ParentCtrlFunction($scope) {
    // use .controller to access properties inside ng-controller
    //in the DOM omit $scope, it is inferred based on the current controller
    $scope.parentProperty = 'parent scope';
}
function ChildCtrlFunction($scope) {
    $scope.childProperty = 'child scope';
    //just like in the DOM, we can access any of the properties in the
    //prototype chain directly from the current $scope
    $scope.fullSentenceFromChild = 'Same $scope: We can access: ' +
    $scope.rootProperty + ' and ' +
    $scope.parentProperty + ' and ' +
    $scope.childProperty;
}  
例如


这是一个简单的流程,我们有rootScope、parentScope、childScope。在每个部分中,我们都分配相应的范围变量。我们可以在parentScope中访问$rootScope,在childScope中访问rootScope和parentScope。

。run在初始化开始时运行一次。我认为,在这一点上使用$scope没有多大意义。你可以传入$rootScope tho。为一个差不多一年前被回答和接受的问题添加一个新答案有什么意义?我想分享我的知识,在这个例子中,它将rootScope解释为parentScope,parentScope解释为childScope,url解释为html代码,我希望这可能对@Tristan和其他Hanks Shekkar有所帮助,请解释您的代码片段的含义。代码片段本身没有帮助。我认为这是最正确的答案,因为它描述了如何解决实际问题。谢谢你@Shekkar!1个问题:如果我在run块中添加任何事件处理程序,比如
$rootScope.on('myevent',function(){})
。。。。我该怎么称呼$destroy?在$rootScope本身上?因为如果我不这样做,我会得到lint错误。。。。并且无法在运行块中使用$scope。
var app = angular.module('myApp', []);
app.run(function ($rootScope) {
    // use .run to access $rootScope
    $rootScope.rootProperty = 'root scope';
});

app.controller("ParentCtrl", ParentCtrlFunction);
app.controller("ChildCtrl", ChildCtrlFunction);
function ParentCtrlFunction($scope) {
    // use .controller to access properties inside ng-controller
    //in the DOM omit $scope, it is inferred based on the current controller
    $scope.parentProperty = 'parent scope';
}
function ChildCtrlFunction($scope) {
    $scope.childProperty = 'child scope';
    //just like in the DOM, we can access any of the properties in the
    //prototype chain directly from the current $scope
    $scope.fullSentenceFromChild = 'Same $scope: We can access: ' +
    $scope.rootProperty + ' and ' +
    $scope.parentProperty + ' and ' +
    $scope.childProperty;
}