代码之家  ›  专栏  ›  技术社区  ›  Akshay Gaikwad

从属性文件加载锚定标记时,ui sref无法正常工作

  •  0
  • Akshay Gaikwad  · 技术社区  · 6 年前

    属性文件中的条目

    MYKEY= <a ui-sref="mystate.state">Go to my page using state</a> and <a href="/#/mypage/page">Go to my page using link/a>
    

    我已从属性文件&应用 $sce.trustAsHtml() 并在html中使用。我可以看到的链接 使用链接转到我的页面 但是 使用状态转到我的页面 没有像以前那样正常工作 ui-sref

    注: ui sref

    1 回复  |  直到 6 年前
        1
  •  2
  •   Naren Murali    6 年前

    这可能是因为内容未经编译,因此 ui-sref 它不起作用。

    SO Answer

    var app = angular.module('myApp', []);
    
    app.controller('MyController', function MyController($scope) {
      $scope.test = function() {
        console.log("it works!");
      }
      $scope.html = '<a ui-sref="mystate.state">Go to my page using state</a> and <a href="/#/mypage/page">Go to my page using link</a><button ng-click="test()">click</button>{{asdf}}';
    });
    
    app.directive('compile', ['$compile', function ($compile) {
        return function(scope, element, attrs) {
            scope.$watch(
                function(scope) {
                    // watch the 'compile' expression for changes
                    return scope.$eval(attrs.compile);
                },
                function(value) {
                    // when the 'compile' expression changes
                    // assign it into the current DOM
                    element.html(value);
                    // compile the new DOM and link it to the current
                    // scope.
                    // NOTE: we only compile .childNodes so that
                    // we don't get into infinite loop compiling ourselves
                    $compile(element.contents())(scope);
                }
            );
        };
    }])
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
    <div ng-controller='MyController' ng-app="myApp">
      <div compile="html"></div>
    </div>