从AngularJS$scope ng模型返回对象

Return object from AngularJS $scope ng-model

本文关键字:模型 返回 对象 ng scope AngularJS      更新时间:2023-09-26

我使用angularjs框架,创建了一个form.html和一个controller.js,其中包含一个检索框SSID的变量。如何在表单中自动分配变量的值。这是一个输入字段。启动应用程序时,表单应自动显示SSID,而用户无需这样做

$scope.SSID {}; return [object Oject] in input form ng-model="SSID"

谢谢你好心帮助我。

Controller.js

/*Controller*/
'use strict';
angular.module('djoro.controllers')
.controller('WifiSmartConfigCtrl', function($scope, $window, $ionicPlatform) {a
    $scope.SSID = {};
    $scope.getSSID = function() {
        var onSuccess = function(SSID) {
            $scope.SSID = SSID;
            return SSID;
        };
      
        var onFail = function() {};
        $ionicPlatform.ready(function() {
            $window.cordova.plugins.Smartconfig.getSSID(onSuccess, onFail, $scope.SSID);
        });
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script>
<ion-pane>
    <ion-header-bar class="bar-stable">
        <h1 class="title">Ionic Blank Starter</h1>
    </ion-header-bar>
    <ion-content ng-controller="WifiSmartConfigCtrl">
        <form novalidate class="simple-form">
            <fieldset>
                <legend>WI-FI</legend>
                <div class="list input-fields">
                    <label class="item item-input">
                        <span class="input-label">SSID :</span>
                        <input type="text" name="test" ng-model="SSID" id="SSID" placeholder="SSID" required show-hide-input>
                    </label>
                    <label class="item item-input" show-hide-container>
                        <span class="input-label">Password :</span>
                        <input type="passwprd" name="test" placeholder="Password" required show-hide-input>
                    </label>
                </div>
            </fieldset>
        </form>
    </ion-content>
</ion-pane>

您已经在获取SSID的作用域上定义了一个函数,但从未从UI中调用。如果你想让UI自动拥有它,你需要在控制器初始化中调用它。

控制器的代码如下所示:

.controller('WifiSmartConfigCtrl', function($scope, $window, $ionicPlatform) {a
    $scope.SSID = {};
    var onSuccess = function(SSID) {
        $scope.SSID = SSID;
        return SSID;
    };
    $ionicPlatform.ready(function() {
        $window.cordova.plugins.Smartconfig
            .getSSID(onSuccess, angular.noop, $scope.SSID);
    });
});
    I use a plugin cordova "smartConfig"
/*plugin.js*/
        function Plugin(){}
    Plugin.alert = function(content){
          var onSuccess = function(){};
          var onFail = function(){};
          cordova.exec(onSuccess, onFail, 'SmartconfigPlugin', 'alert', [content]);
    };

    Plugin.getSSID = function(onSuccess, onFail){
          cordova.exec(onSuccess, onFail, 'SmartconfigPlugin', 'getSSID', []);
    };
    module.exports = Plugin;

SmartConfig.java

package fr.enersy.cordova.smartconfig;
import org.json.JSONArray;
import org.json.JSONException;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import android.R;
import android.content.Context;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.util.Log;
import com.integrity_project.smartconfiglib.SmartConfig;
import com.integrity_project.smartconfiglib.SmartConfigListener;
import com.pandaos.smartconfig.utils.NetworkUtil;
//import fr.enersy.cordova.smartconfig.mySmartconfigListener;   // TODO remove from config.xml
public class SmartconfigPlugin extends CordovaPlugin {
  // Variables declaration
  SmartConfigListener smartConfigListener;
  SmartConfig smartConfig;
  byte[] freeData;
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if("alert".equals(action)){
      final String content = args.getString(0);
      showAlert(content);
      callbackContext.success();
      return true;
    }
    else if("getSSID".equals(action)){
        String SSID = getSSID();
        callbackContext.success(SSID);
        return true;
      }
  }
  private String getSSID() {
      Log.i("--->> SmartconfigPlugin", "Enter getSSID");
      String SSID = (NetworkUtil.getWifiName(this.cordova.getActivity())).trim();   // TODO replace by something like: smartconfig_network_name_field.getText().toString().trim();
      Log.i("---------->> SmartconfigPlugin", "SSID: " + SSID);
      Log.i("---------->> SmartconfigPlugin", "Exit getSSID");
      return SSID;
  }
}