auto switch toggle off if one is currently on

后端 未结 2 533
栀梦
栀梦 2020-12-22 13:02

there are nested toggle buttons which are off by default, when user turns one on it saves a value to localstorage. What i want to do is when one is turned on and you try to

2条回答
  •  眼角桃花
    2020-12-22 13:42

    I'm finding it hard to put the second js into the first one. I'm kinda confused since i'm new to angularjs

    It sounds like you're trying to break up your app into separate JS files. Taking a tip from the Angular 1.x [Code] Style Guide on how you should break up your app, I would create a singular module setter file where you define the module and declare any module dependencies.

    module.js

    angular.module( 'app', [ ... ])
    

    controller.js

    So you're 1st and 2nd scripts are for the same controller as best I can tell. You do need to settle on one name or the other. I recommend 'Shops' as that is a standard naming convention for angular controllers. Just combine them like so:

    angular.module( 'app' )
    .controller( 'Shops', [
        '$scope','$http','$timeout',
        function( $scope, $http, $timeout ){
            $http.get('http://localhost/moves/templates/shopping.php')
            .success(function(data){ .... })
    
    
            $scope.pushNotification = {};
            $scope.pushNotification.text = "Sample"
            $scope.pushNotification.checked = false;
    
            $scope.pushNotificationChange = function(item) {
                   console.log('Push Notification Change',         $scope.pushNotification.checked);
                if($scope.pushNotification.checked){
                    localStorage.setItem("shop_id",($scope.item.shop_id));
                }else{
                    localStorage.removeItem("shop_id");
                }
            }
    
    
             $scope.shops = [
              { shop_id: 1, shop_name: 'Shop One', checked: false },
              { shop_id: 1, shop_name: 'Shop One', checked: false },
              ...
             ]
    
             $scope.onItemChange = function(item) { ... }
    

    Lastly you need to be sure to declare your module.js before your controller.js. I highly recommend a few things to you, seeing as you are new to Angular:

    • start using the controller-as syntax
    • start following some of the conventions outlined in that style guide.

    I say this because if you plan to move to Angular 2 at some point, being well-practiced in these conventions will ease the migration process.

提交回复
热议问题