NFC写样本

时间:2015-06-16 03:50:11

标签: ionic-framework nfc

请将$scope数据传递到nfc.write函数时出现问题,但如果我传入一个文字硬编码值,我需要编写如何编写用户输入数据的示例解决方案NFC Tag,我尝试过的所有内容都没有成功,我所能做的就是从$scope用户数据中添加数据。

这就是我所做的一切

HTML

<ion-view view-title="Chats" data-ng-controller="chat as vm">
<ion-content>

    <div class="list">
        <label class="item item-input">
            <span class="input-label">Username</span>
            <input type="text" ng-model="userData.userName">
        </label>

        <label class="item item-input">
            <span class="input-label">Description</span>
            <input type="text" ng-model="userData.description">
        </label>
    </div>

    <fieldset style="clear:both;">
        <input data-ng-click="login()" type="submit" name="submit"
               value="Login" class="more blue" />
    </fieldset>

    {{userData}}
</ion-content>

控制器:

.controller('chat', function($scope, $cordovaNfc) {

$scope.loginData = {}; // This should get the data from view to be used below

$cordovaNfc.then(function(nfc){

    // I need to find a way to pass in the userData this point
    console.log($scope.loginData); // Here am not getting the data;

    var payloadData = $scope.loginData; // And i did get anything in the payload

    //Use the plugins interface as you go, in a more "angular" way
    nfc.addNdefListener(function(nfcEvent){

        var tnf = ndef.TNF_EXTERNAL_TYPE,   
            recordType = "application/json", 
            payload = payloadData,                
            record,                               
            message = [
               ndef.mimeMediaRecord(recordType, payload)  
            ];            

        nfc.write(
           message,   // write the record itself to the tag
           function (success) { 
               console.log("Wrote data to tag.");
           },
           // this function runs if the write command fails:
           function (reason) {
               alert("There was a problem " + reason);
           }
        );

    }).then(
    //Success callback
    function(nfcEvent){
        console.log("bound success");
    },
    //Fail callback
    function(err){
        console.log("error");
        alert("error");
    });
   });
  })

感谢。

1 个答案:

答案 0 :(得分:0)

在模板中,您的模型是userData(=&gt; $ scope.userData)。 在您的控制器中,您希望将$ scope.loginData写入控制台未定义的内容(或者可能为NULL)。所以你的log和payloadData变量都是空的。