Javascript 如何在视图中使用knockout.js ViewModel?

Javascript 如何在视图中使用knockout.js ViewModel?,javascript,knockout.js,viewmodel,Javascript,Knockout.js,Viewmodel,我被分配了这个任务来纠正谷歌地图的网址 目前,地图指示用户从当前地理位置,而不是输入的邮政编码(新位置…lat和long) 这是我的视图模型 function ViewModel() { var self = this; self.map = null; self.currentLocation = { latitude: 0, longitude: 0 }; self.geocode = function () {

我被分配了这个任务来纠正谷歌地图的网址

目前,地图指示用户从当前地理位置,而不是输入的邮政编码(新位置…lat和long)

这是我的视图模型

function ViewModel() {
    var self = this;
    self.map = null;
    self.currentLocation = {
        latitude: 0,
        longitude: 0
    };

    self.geocode = function () {
        if (!self.isValid()) {
            self.zipCode(self.previousZipCode);
        } else {
            self.previousZipCode = self.zipCode();
        }

        // Take the updated zip code and geocode it into lat/long
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'address': self.zipCode() }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                lat = results[0].geometry.location.lat();
                lng = results[0].geometry.location.lng();

                var newLocation = {
                    latitude: lat,
                    longitude: lng
                };

                if (self.currentLocation.latitude == newLocation.latitude && self.currentLocation.longitude == newLocation.longitude) return;
                self.currentLocation = newLocation;

                self.reload();
然后,在视图中。。。我只是不知道如何在标记(视图)中获取currentLocation值



只需执行
…映射/dir/'+currentLocation.latitude+','+currentLocation.longitude
。虽然,你正在计划这个价值的改变,但是你应该考虑让它像一个“MikEC状态”,你不是在处理可观测物。你没有必要用击倒的方式来做这件事。让您的ViewModel准备URL,以便在位置更改时更新URL。谢谢大家。。。我认为可能是这样的,但我没有写这篇文章,而且写这篇文章的人知识渊博,所以我认为他们的设置是正确的。
  <a data-bind="attr: {'href' : 'https://www.google.com/maps/dir/**Current+Location**/' + Latitude + ',' + Longitude, 'data-track' : 'Find a Location', 'data-track-action' : 'directions', 'data-track-label' : ItemName }" target="_blank" class="locations-icon flex-item uppercase">
                            <i class="material-icons">directions</i><br />
                            directions
                        </a>