简体   繁体   中英

How to use AngularJS $rootScope?

I am passing three $rootScope from process controller to Rating controller so based on the $rootScope status i am enabling and disabling buttons. Edit and view is working good but on $rootScope === 'NewPrt' , Once user answered all question i want to enable submit button on 'NewPrt' .

So far i tried below code..

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;
      }

You can try like this instead of using $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> 

When both conditions are true, enable the submit button:

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

if you want to use $rootScope you need to inject $rootScope in every controller where you want to use config run any where

like this

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) 
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM