html 在 ng-init 中添加标签

html tag with in ng-init

我是angularjs初学者。我试图做这样的事情:

    <div ng-init="elements=
        [
          {
            element:'<a herf='www.google.com'>google</a>'
          },
          {
            element:'<a herf='www.yahoo.com'>yahoo</a>'
          }
        ]">
          <div ng-repeat="item in elements">
              <p>{{item.element}}</p>
          </div>
    </div>

它不起作用,我认为这是因为 a 标记内的单引号。有什么解决方法吗?

就像@PSL 提到的那样,您应该将该逻辑移至控制器中:

$scope.elements = [
          {
            element:'<a href="www.google.com">google</a>'
          },
          {
            element:'<a href="www.yahoo.com">yahoo</a>'
          }
        ]

如果您想将它保留在 ng-init 中,问题是您的 href='...' 应该像上面的代码一样用双引号引起来。为了使用双引号,您需要像这样转义它们:\"。您还拼错了 href.

来自AngularJS docs:

"The only appropriate use of ngInit is for aliasing special properties of ngRepeat, as seen in the demo below. Besides this case, you should use controllers rather than ngInit to initialize values on a scope."