如何使用AngularJS $ rootScope?

时间:2015-03-24 13:39:53

标签: javascript angularjs

我正在将三个$rootScope从过程控制器传递到评级控制器,因此基于我启用和禁用按钮的$rootScope状态。编辑和查看工作正常,但在$rootScope === 'NewPrt'上,一旦用户回答了我要在'NewPrt'上启用提交按钮的所有问题。

到目前为止,我尝试了以下代码..

HTML

<button type="submit" class="btn btn-default" ng-disabled="disabledDraft"  ng-click="savePRTDraft()" ng-show="showSaveDraftBtn">Save
        as Draft</button>
    <button type="submit" class="btn btn-primary"
        ng-disabled="disableSubmitButton" ng-click="submitClicked()">Submit</button>

ProcessCtrl.js

$scope.gotoQstnPage = function(isNew) {
        var qrtUrl = "/createRtgQstnAir/"+$scope.processDTO.processKey + "/"+isNew;
        $rootScope.status = 'NewPrt';
        $location.path(qrtUrl);
    }

$scope.editProcessRating = function(prcsSessionKey) {
            var prtUrl = "/getProcessRating/"+prcsSessionKey;
            $rootScope.status = 'edit';
            $location.path(prtUrl);

        }

        $scope.viewProcessRating = function(prcsSessionKey) {
          var prtUrl = "/getProcessRating/"+prcsSessionKey;
          $rootScope.status = 'view';
          $location.path(prtUrl);
        }

RatingCtrl.js

if(j > $scope.questionnaire.length){
              if($rootScope.status ==='edit') {
                $scope.disableSubmitButton = false;
                $scope.disabledDraft = false;
                $scope.showBusDecDropDown = true;
              }

 $scope.disabledDraft = function(){
        if($rootScope.status === 'view') {
          return true;
        }
        else {
          return false;
        }
      }
      if ($rootScope.status === "NewPrt" ) {
        $scope.disabledDraft = false;
      }

3 个答案:

答案 0 :(得分:1)

您可以尝试这样做,而不是使用$rootScope

var app = angular.module('myApp', []);
app.controller('Controller', function ($scope) {

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='myApp' ng-controller="Controller">
    <form name="myForm">
        <input name="myText" type="text" ng-model="mytext" required />
        <button ng-disabled="myForm.$invalid">Save</button>
    </form>
</div>

答案 1 :(得分:1)

当两个条件都为真时,启用提交按钮:

if ($rootScope ==='edit' || $rootScope ==='NewPrt') {
    $scope.disableSubmitButton = false;
}

答案 2 :(得分:0)

如果你想使用$ rootScope,你需要在每个想要使用配置运行的控制器中注入$ rootScope

像这样

var app = angular.module('app');
app.config(function($rootScope){
  $rootScope.name = "Hello World"
});

app.controller('home',function($scope,$rootScope){
$scope.name = $rootScope.name;
alert($scope.name) 
})