简体   繁体   English

在AngularJS选项卡式视图中设置默认的单选/输入/复选框值

[英]Set default radio/input/checkbox value in AngularJS tabbed view

I have an AngularJS app using Ionic Framework which has a tabbed view with a Settings tab which will store user options for use. 我有一个使用Ionic Framework的AngularJS应用,该应用具有带设置选项卡的选项卡式视图,该选项卡将存储要使用的用户选项。 I want to use these settings when the app initially launches and while I could use the stored value in localStorage, I want to be able to check the options are set on the settings tab. 我想在应用程序最初启动时使用这些设置,并且可以使用localStorage中的存储值,但我希望能够检查设置选项卡上的选项。

As it stands now, if you load the settings tab, you'll see one of the radio buttons sets itself from off to on as you load, and this is what happens whenever I set a default value that is on. 就目前而言,如果您加载设置标签,您会看到其中的单选按钮之一在加载时将其自身设置为从关闭到打开,这是每当我将默认值设置为on时发生的情况。 How can I get this to be set when the app launches and not when the tab itself is opened? 如何启动应用程序而不是打开选项卡本身时进行设置?

This is what my controllers.js looks like: 这是我的controllers.js的样子:

angular.module('starter.controllers', [])

.controller('DashCtrl', function($scope, $window, $ionicPlatform) {

    $ionicPlatform.ready(function() {
        if ( ! $window.localStorage.getItem( 'grossoption' ) ) {
        $window.localStorage.setItem( 'grossoption', 'year' );
    }

    if ( ! $window.localStorage.getItem( 'resultoption' ) ) {
        $window.localStorage.setItem( 'resultoption', 'month' );
    }
    });

    $scope.grossOptionsList = [
    { text: "Year", value: "year" },
    { text: "Month", value: "month" },
    { text: "Week", value: "week" },
    { text: "Day", value: "day" }
    ];

    $scope.resultsOptionsList = [
    { text: "Year", value: "year" },
    { text: "Month", value: "month" },
    { text: "Week", value: "week" },
    { text: "Day", value: "day" }
    ];

// Default values
    $scope.data = {};
    $scope.data.grossOptions = 'year';
    $scope.data.resultOptions = 'month';


    $scope.updateGrossOptions = function(item) {
      $window.localStorage.setItem( 'grossoption', item.value );
        console.log( 'Gross option: ' + item.value );
    }

    $scope.updateResultOptions = function(item) {
      $window.localStorage.setItem( 'resultoption', item.value );
        console.log( 'Results option: ' + item.value );
    }

})

.controller('SettingsCtrl', function($scope, $window, $ionicPlatform) {
$ionicPlatform.ready(function() {


    if ( ! $window.localStorage.getItem( 'toggle1' ) ) {
        $window.localStorage.setItem( 'toggle1', 'true' );
    }

    if ( ! $window.localStorage.getItem( 'toggle2' ) ) {
        $window.localStorage.setItem( 'toggle2', 'false' );
    }


});

$scope.data = {};
$scope.data.toggle1 = $window.localStorage.getItem( 'toggle1' ) === "true";
$scope.data.toggle2 = $window.localStorage.getItem( 'toggle2' ) === "true";

$scope.update_toggle1 = function() {
    $window.localStorage.setItem( 'toggle1', $scope.data.toggle1 );
    console.log( 'Toggle 1: ' + $scope.data.toggle1 );
}

$scope.update_toggle2 = function() {
    $window.localStorage.setItem( 'toggle2', $scope.data.toggle2 );
    console.log( 'Toggle 2: ' + $scope.data.toggle2 );
}

}) I've set up an example plnkr here which shows the behaviour my app currently has when you load the Settings tab. })我在这里设置了一个示例plnkr,示例显示了在加载“设置”标签时我的应用当前具有的行为。

Appreciate any feedback on how best to approach this in Angular. 感谢有关如何最好地在Angular中解决此问题的任何反馈。

您可以在“ ng-checked”中使用“ if”条件,如下所示:

<ion-toggle ng-checked="(myVar== 1)?true:false;" ng-change="myFunc(myVar)" ng-model="myVar" toggle-class="toggle-calm">{{myVar}}</ion-toggle>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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