AngularJS:通过JS在输入框内写入,不要在JSON中传递值

时间:2016-02-19 02:12:16

标签: javascript angularjs json inputbox

背景:我有一个外部设备(条形码阅读器),当用户扫描某些内容时,它会将信息发送回平板电脑。我订阅了频道,我需要将值放在当前关注的单元格内并将其写入。

错误:我可以捕获订阅并在输入框中直观地写入值,但它永远不会到达下面的JSON 。 我也试过$scope.$apply()但它没有改变任何东西(也许我用错了)。

"Working" Plunker with the problem

$scope.randomClickOnStuff = function() {
    // Here Randomely publish stuff with value so we can write it in specific field.
    window.setTimeout(function() {
        if (!$scope.stopMe) {
            vm.objectOtSubscribeTo.publish(channelToUse, Date.now());
            $scope.randomClickOnStuff();
        } else {
            // Stop the loop.
        }
    }, 1000);
};

var callbackCompleted = function(resultValue) {
    // Important code Here
    // Code to write in the input box here.

    console.log(resultValue);
    if (document.activeElement.localName == "input") {
        // Option 1:
        //--> Work Visually <-- but do not put the value inside the JSON.
        document.activeElement.value = resultValue;
        $scope.$apply();

        // Option 2:
        // http://stackoverflow.com/questions/11873627/angularjs-ng-model-binding-not-updating-when-changed-with-jquery
        // Problem: The "document.activeElement.attributes['ng-model'].value" is not link with the scope, but with the ng-repeat row. So I have access to the Scope, but not the Row item.
       //var binding = document.activeElement.attributes['ng-model'].value;
       // Rule: I might not know where the Item is so I cannot do $scope.complexObject[row][binding]
    } else {
        console.log("not inside a Input box.");
    }
};

vm.objectOtSubscribeTo.subscribe(channelToUse, callbackCompleted);

由于

1 个答案:

答案 0 :(得分:1)

一种解决方案是通过将其设置在其中一个单元格的焦点上来跟踪所选行和单元格

public IntPtr Click()

    {
        int hr;

        // get ready to wait for new image
        m_PictureReady.Reset();
        m_ipBuffer = Marshal.AllocCoTaskMem(Math.Abs(m_stride) * m_videoHeight);

        try
        {
            m_WantOne = true;

            // If we are using a still pin, ask for a picture
            if (m_VidControl != null)
            {
                // Tell the camera to send an image
                hr = m_VidControl.SetMode(m_pinStill, VideoControlFlags.Trigger);
                DsError.ThrowExceptionForHR(hr);
            }

            // Start waiting
            if (!m_PictureReady.WaitOne(9000, false))
            {
                throw new Exception("Timeout waiting to get picture");
            }
        }
        catch
        {
            Marshal.FreeCoTaskMem(m_ipBuffer);
            m_ipBuffer = IntPtr.Zero;
            throw;
        }

        // Got one
        return m_ipBuffer;
    }

示例:https://plnkr.co/edit/och5PoepJuRde0oONIjm?p=preview