Javascript AngularJS:模板中的访问注入控制器

Javascript AngularJS:模板中的访问注入控制器,javascript,angularjs,Javascript,Angularjs,我有两个控制器,ItemController和AuthenticationController AuthenticationController被注入ItemController ItemModule.controller('ItemController', function ($scope, AuthenticationController) 我现在试图从模板中调用AuthenticationController中的isLoggedIn()函数 我该怎么做呢?一种方法是将IsLoggedIn

我有两个控制器,
ItemController
AuthenticationController

AuthenticationController
被注入
ItemController

ItemModule.controller('ItemController', function ($scope, AuthenticationController)
我现在试图从模板中调用
AuthenticationController
中的
isLoggedIn()
函数


我该怎么做呢?

一种方法是将
IsLoggedIn
方法放在
$scope
上,如下所示:

$scope.isLoggedIn = AuthenticationController.isLoggedIn
现在可以在模板中调用此函数

<span>Logged In: <strong>{{IsLoggedIn()}}</strong></span>
登录:{{IsLoggedIn()}

我不确定注入控制器是否是一种好的做法,因为每个控制器都应该关注自己的部分。如果您想在他们之间共享信息,应该使用服务。而不是注入控制器;你可以把它们放在你的前端。例如:

// js
app.controller('ParentController', function($scope) {
    $scope.person = {greeted: false};
});

app.controller('ChildController', function($scope) {
    $scope.sayHello = function() {
    $scope.person.name = "Ari Lerner";
    $scope.person.greeted = true;
    }
});

// html
<div ng-controller="ParentController">
    <div ng-controller="ChildController">
        <a ng-click="sayHello()">Say hello</a>
    </div>
    {{ person }}
</div>

// displayed:
{greeted:true, name: "Ari Lerner"}
//js
app.controller('ParentController',函数($scope){
$scope.person={helleed:false};
});
app.controller('ChildController',函数($scope){
$scope.sayHello=函数(){
$scope.person.name=“Ari Lerner”;
$scope.person.helleed=true;
}
});
//html
打招呼
{{person}}
//显示:
{问候:对,名字:“Ari Lerner”}
您需要