首页 \ 问答 \ 自定义指令在ng-if中第二次没有正确绑定(Custom directive not binding correctly in ng-if second time around)

自定义指令在ng-if中第二次没有正确绑定(Custom directive not binding correctly in ng-if second time around)

我有一个自定义指令,它使用双向绑定到我的控制器(使用'=')。

<my-streetview 
    latitude="quickView.streetView.latitude" 
    longitude="quickView.streetView.longitude" 
    ng-if="quickView.activeTab === 'street'"
></my-streetview>

我正在使用ng-if因为我没有加载谷歌地图/街景,直到它所在的标签打开。 麻烦的是它第一次显示一切正常但第二次ng-if为真(当你点击另一个标签然后返回标签时)它似乎将long和lat设置为undefined。

我知道以下内容:

A)如果我将ng-hide更改为ng-show它就可以了。 每次离开时,谷歌地图都不会被销毁并创建,因此这是有道理的。 这与ng有关 - 如果在我认为错误的情况下摧毁了某些东西。

B)在父控制器中实际上改变了lat和long值,因为我在那里放了一个$ watch和一个console.log()来测试它。 基本上,当ng-if设置为true时,第一次创建街景视图时,第二次和后续时间它无法从父控制器读取值,或者实际上将它们设置为未定义。

C)第一次显示ng-if和随后的时间之间没有其他任何不同。 没有其他我能想到的代码可以发挥作用。

真的很感激任何想法。

这是我的街景指令的完整代码。

angular.module('my.directives')

.constant('myStreetviewDefaults', {

    latitude: 51.816102,
    longitude: -0.811619

})


.directive('myStreetview', function ($timeout, myStreetviewDefaults) {

    return {

        restrict: 'EA',
        scope: {
            latitude: '=',
            longitude: '='
        },


        link: function ($scope, $element, $attrs) {

            $scope.latitude = angular.isDefined($scope.latitude) ? $scope.$eval($scope.latitude) : myStreetviewDefaults.latitude;
            $scope.longitude = angular.isDefined($scope.longitude) ? $scope.$eval($scope.longitude) : myStreetviewDefaults.longitude;


            // Create the panorama
            var mapEl = $('<my-streetview-map></my-streetview-map>');
            mapEl.addClass('my-streetview-map-container');


            $element.append(mapEl);

            var panorama = new google.maps.StreetViewPanorama(mapEl[0], {
                position: {
                    lat: $scope.latitude,
                    lng: $scope.longitude
                },
                pov: {
                    heading: 34,
                    pitch: 10
                }
            });


            // Watch latitude and longitude to reset the center
            $scope.$watchGroup(['latitude','longitude'], function (newValues, oldValues, $scope) {

                panorama.setPosition({
                    lat: $scope.latitude,
                    lng: $scope.longitude
                });

            });


            // Hack to get street view drawing properly on second load
            // https://github.com/allenhwkim/angularjs-google-maps/issues/59
            $timeout(function(){
                google.maps.event.trigger(panorama,'resize'); 
            }, 100);

        }


    };


});

这是街景视图所在的Angular UI模式的控制器代码。

angular.module('app')

.controller('QuickViewCtrl', function ($rootScope, $scope, $log, $http, appConfig, $u, $modalInstance, modalSettings) {

    'use strict';

    var master = $scope.master;

    var quickView = this;


    $log.info('Quick View Opened', modalSettings);


    this.close = function(){
        $modalInstance.close();
    }

    ///////////////////////////////////////////////////////////////
    //  Initialize Page
    ///////////////////////////////////////////////////////////////

    var init = function () {

        // Set the initial tab
        quickView.activeTab = modalSettings.initialPanel;


        // Set up the street view
        quickView.streetView = {
            latitude: modalSettings.property.latitude,
            longitude: modalSettings.property.longitude
        };


        $scope.$watch('quickView.streetView', function(newValues, oldValues){
            console.log("Test watching from controller", newValues);
        }, true);



    };

    init();

});

这是模态窗口的模板....

<div class="quickView modal-inner modal--has-header modal--has-footer">

    <!-- Header -->
    <div class="modal-header">

        <!-- Header removed for brevity -->

    </div>
    <div class="modal-main">

        <!-- Tabs -->
        <my-tabset
            my-tabset-active-tab="quickView.activeTab" 
        >
            <div my-tabset-tabs my-tabset-tabs--equal4>
                <a href="#" my-tabset-tab my-tabset-tab-name="overview" is-active="true">
                    <div my-tabset-tab-text>Overview</div>
                </a>
                <a href="#" my-tabset-tab my-tabset-tab-name="gallery">
                    <div my-tabset-tab-text>Gallery</div>
                </a>
                <a href="#" my-tabset-tab my-tabset-tab-name="map">
                    <div my-tabset-tab-text>Map</div>
                </a>
                <a href="#" my-tabset-tab my-tabset-tab-name="street">
                    <div my-tabset-tab-text>Street View</div>
                </a>
            </div>
            <div my-tabset-panels>

                <!-- Overview Panel -->
                <div my-tabset-panel my-tabset-tab-name="overview" is-active="true">

                    <div ng-if="quickView.activeTab === 'overview'">

                        <!-- Overview removed for brevity -->

                    </div>

                </div>

                <!-- Gallery Panel -->
                <div my-tabset-panel my-tabset-tab-name="gallery">

                    <div ng-if="quickView.activeTab === 'gallery'">

                        <!-- Gallery removed for brevity -->

                    </div>

                </div>

                <!-- Map Panel -->
                <div my-tabset-panel my-tabset-tab-name="map">

                    <ui-gmap-google-map 
                        center='quickView.map.center' 
                        zoom='quickView.map.zoom' 
                        options="quickView.map.options" 
                        control="quickView.mapControl" 
                        ng-if="quickView.activeTab === 'map'"
                    >
                        <ui-gmap-marker
                            idKey="'quickViewMapMarker'" 
                            coords='quickView.map.markerPosition'
                        >
                        </ui-gmap-marker>
                    </ui-gmap-google-map>

                </div>

                <!-- Street View Panel -->
                <div my-tabset-panel my-tabset-tab-name="street">

                    <my-streetview 
                        latitude="quickView.streetView.latitude" 
                        longitude="quickView.streetView.longitude" 
                        ng-if="quickView.activeTab === 'street'"
                    ></my-streetview>

                </div>
            </div>
        </my-tabset>

    </div>


    <!-- Footer -->
    <div class="modal-footer">
        Footer
    </div>

</div>

I have a custom directive which is using two way binding to my controller (using '=').

<my-streetview 
    latitude="quickView.streetView.latitude" 
    longitude="quickView.streetView.longitude" 
    ng-if="quickView.activeTab === 'street'"
></my-streetview>

I'm using ng-if because I don't wnt the google map/streetview being loaded until the tab which it is in is opened. The trouble is that the first time it shows everything works but the second time the ng-if is true (when you click a different tab then come back to the tab) it seems to set the long and lat to undefined.

I know the following:

A) if I change ng-hide to ng-show it just works. The google map is not being destroyed and created every time you leave nad return to the tab so this makes sense. It's something to do with ng-if destroying something when it's false I think.

B) The lat and long values DO actually get changed in the parent controller because I put a $watch and a console.log() in there to test it. Basically when the ng-if is set to true the first time it creates the streetview fine, the second and subsequent times it either cannot read the values from the parent controller, or is actually setting them to undefinied.

C) Nothing else is different between the first time the ng-if is shown and subsequent times. There is no other code I can think of coming into play.

Any ideas would really be appreciated.

Here is my full code for my street view directive.

angular.module('my.directives')

.constant('myStreetviewDefaults', {

    latitude: 51.816102,
    longitude: -0.811619

})


.directive('myStreetview', function ($timeout, myStreetviewDefaults) {

    return {

        restrict: 'EA',
        scope: {
            latitude: '=',
            longitude: '='
        },


        link: function ($scope, $element, $attrs) {

            $scope.latitude = angular.isDefined($scope.latitude) ? $scope.$eval($scope.latitude) : myStreetviewDefaults.latitude;
            $scope.longitude = angular.isDefined($scope.longitude) ? $scope.$eval($scope.longitude) : myStreetviewDefaults.longitude;


            // Create the panorama
            var mapEl = $('<my-streetview-map></my-streetview-map>');
            mapEl.addClass('my-streetview-map-container');


            $element.append(mapEl);

            var panorama = new google.maps.StreetViewPanorama(mapEl[0], {
                position: {
                    lat: $scope.latitude,
                    lng: $scope.longitude
                },
                pov: {
                    heading: 34,
                    pitch: 10
                }
            });


            // Watch latitude and longitude to reset the center
            $scope.$watchGroup(['latitude','longitude'], function (newValues, oldValues, $scope) {

                panorama.setPosition({
                    lat: $scope.latitude,
                    lng: $scope.longitude
                });

            });


            // Hack to get street view drawing properly on second load
            // https://github.com/allenhwkim/angularjs-google-maps/issues/59
            $timeout(function(){
                google.maps.event.trigger(panorama,'resize'); 
            }, 100);

        }


    };


});

This is the controller code for the Angular UI modal that the streetview sits inside.

angular.module('app')

.controller('QuickViewCtrl', function ($rootScope, $scope, $log, $http, appConfig, $u, $modalInstance, modalSettings) {

    'use strict';

    var master = $scope.master;

    var quickView = this;


    $log.info('Quick View Opened', modalSettings);


    this.close = function(){
        $modalInstance.close();
    }

    ///////////////////////////////////////////////////////////////
    //  Initialize Page
    ///////////////////////////////////////////////////////////////

    var init = function () {

        // Set the initial tab
        quickView.activeTab = modalSettings.initialPanel;


        // Set up the street view
        quickView.streetView = {
            latitude: modalSettings.property.latitude,
            longitude: modalSettings.property.longitude
        };


        $scope.$watch('quickView.streetView', function(newValues, oldValues){
            console.log("Test watching from controller", newValues);
        }, true);



    };

    init();

});

And this is the template for the modal window....

<div class="quickView modal-inner modal--has-header modal--has-footer">

    <!-- Header -->
    <div class="modal-header">

        <!-- Header removed for brevity -->

    </div>
    <div class="modal-main">

        <!-- Tabs -->
        <my-tabset
            my-tabset-active-tab="quickView.activeTab" 
        >
            <div my-tabset-tabs my-tabset-tabs--equal4>
                <a href="#" my-tabset-tab my-tabset-tab-name="overview" is-active="true">
                    <div my-tabset-tab-text>Overview</div>
                </a>
                <a href="#" my-tabset-tab my-tabset-tab-name="gallery">
                    <div my-tabset-tab-text>Gallery</div>
                </a>
                <a href="#" my-tabset-tab my-tabset-tab-name="map">
                    <div my-tabset-tab-text>Map</div>
                </a>
                <a href="#" my-tabset-tab my-tabset-tab-name="street">
                    <div my-tabset-tab-text>Street View</div>
                </a>
            </div>
            <div my-tabset-panels>

                <!-- Overview Panel -->
                <div my-tabset-panel my-tabset-tab-name="overview" is-active="true">

                    <div ng-if="quickView.activeTab === 'overview'">

                        <!-- Overview removed for brevity -->

                    </div>

                </div>

                <!-- Gallery Panel -->
                <div my-tabset-panel my-tabset-tab-name="gallery">

                    <div ng-if="quickView.activeTab === 'gallery'">

                        <!-- Gallery removed for brevity -->

                    </div>

                </div>

                <!-- Map Panel -->
                <div my-tabset-panel my-tabset-tab-name="map">

                    <ui-gmap-google-map 
                        center='quickView.map.center' 
                        zoom='quickView.map.zoom' 
                        options="quickView.map.options" 
                        control="quickView.mapControl" 
                        ng-if="quickView.activeTab === 'map'"
                    >
                        <ui-gmap-marker
                            idKey="'quickViewMapMarker'" 
                            coords='quickView.map.markerPosition'
                        >
                        </ui-gmap-marker>
                    </ui-gmap-google-map>

                </div>

                <!-- Street View Panel -->
                <div my-tabset-panel my-tabset-tab-name="street">

                    <my-streetview 
                        latitude="quickView.streetView.latitude" 
                        longitude="quickView.streetView.longitude" 
                        ng-if="quickView.activeTab === 'street'"
                    ></my-streetview>

                </div>
            </div>
        </my-tabset>

    </div>


    <!-- Footer -->
    <div class="modal-footer">
        Footer
    </div>

</div>

原文:https://stackoverflow.com/questions/32076906
更新时间:2021-12-14 17:12

最满意答案

使用unicode转义序列,而不是字符实体:

FalsePositiveInput.Content = "\u2713";

Use a unicode escape sequence, rather than a character entity:

FalsePositiveInput.Content = "\u2713";

相关问答

更多
  • 使用unicode转义序列,而不是字符实体: FalsePositiveInput.Content = "\u2713"; Use a unicode escape sequence, rather than a character entity: FalsePositiveInput.Content = "\u2713";
  • 要构造trie,可以将Unicode字符串编码为UTF-8,然后使用编码的字节序列构造trie。 或者您可以使用代码点,并在节点中使用哈希映射。 您必须对应用程序进行基准测试,以确定哪种方法最有效。 但困难的问题是如何确定两个字符串何时匹配 。 考虑一下café这个词 这可以表示为: A = [U+0063 U+0061 U+0066 U+0065 U+0301] (以e和组合重音结束 ) 但也如 B = [U+0063 U+0061 U+0066 U+00E9] (以é结尾,组合形式) 所以: 字符串是否 ...
  • 从PHP 7开始,您可以使用以下语法: $str = "\u{####}"; 在此之前,您可以使用UTF-8字节值使其显而易见: $str = "\x##\x##"; 但就我个人而言,我会做作者所做的,只需在评论中写下来。 From PHP 7 onwards you can use this syntax: $str = "\u{####}"; Before that, you could use the UTF-8 byte values to make it dead obvious: $str ...
  • 不知道这是否有帮助,但Windows支持泰米尔语代码页“57004 - ISCII泰米尔语”。 尽管如此,它并没有为上面的示例字符提供相同的翻译。 对'''它给出了216.也许需要使用不同的代码页? string tamilUnicodeString = "ஹ"; Encoding encoding = Encoding.GetEncoding("x-iscii-ta"); byte[] codepageBytes = encoding.GetBytes( ...
  • 为什么你不保存/服务CSS文件为UTF-8? nav a:hover:after { content: "↓"; } 如果这还不够好,而且你想保持它全ASCII: nav a:hover:after { content: "\2193"; } 字符串中Unicode字符的一般格式为\000000至\FFFFFF - 反斜杠后跟六个十六进制数字。 当Unicode字符是字符串中的最后一个字符或在Unicode字符后添加空格时,可以省略前导0数字。 有关详细信息,请参阅下面的规范。 CSS2 ...
  • 引用关于isDigit的Java文档 : 如果由getType(codePoint)提供的通用类别类型为DECIMAL_DIGIT_NUMBER,则字符为数字。 所以,我相信匹配数字的模式应该是\p{Nd} 。 这是ideone的一个实例 。 正如你所看到的, Pattern.matches和Character.isDigit的结果是一致的。 Quoting the Java docs about isDigit: A character is a digit if its general category ...
  • 在您的原始查询之前发送此查询:(用于设置mysql连接器的charset) mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'") or die(); 也许它对你有所帮助...... 或者检查脚本源的Cha ...
  • 超出U + FFFF的字符不能直接写成\u....文字(如果你试过,只使用前四个十六进制数字,你得到一个错误的字符)但作为代理对或更容易,使用8位\U字面,例如U + 1F60A的'\U0001F60A' 。 Characters beyond U+FFFF cannot be directly written as \u.... literals (if you try that, only the first four hex digits are used, and you get a wrong c ...
  • StringLiterals.jl允许您执行此操作。 通过s = f"\N{SECTION SIGN}" StringLiterals.jl刚刚注册了。 你可以通过Pkg.add("StringLiterals")安装它 例子 julia> using StringLiterals julia> s = f"\N{SECTION SIGN}" #Unicode Entity "§" julia> f"\N{SNOWMAN}" #Unicode Entity "☃" julia> f"\:mage:" ...
  • 原来是字体问题。 与其他UI控件相比,我使用了与表格单元格不同的字体。 rmaddy上面的评论帮助我解决了这个问题。 It turned out to be font issue. I was using a different font for the table cell as compared to other UI controls. rmaddy's comment above helped me resolve the issue.

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。