简体   繁体   English

带有ng-init的html标签

[英]html tag with in ng-init

I am angularjs beginner. 我是angularjs初学者。 I was trying to do something like this: 我正在尝试做这样的事情:

    <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>

It doesn't work, I think it is because of the single quotes inside the a tag. 它不起作用,我认为这是因为a标记内的单引号引起的。 Is there any workaround? 有什么解决方法吗?

Like @PSL mentioned, you should move that logic into a controller: 就像提到的@PSL一样,您应该将该逻辑移到控制器中:

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

If you want to keep it in ng-init , the issue was that your href='...' , should be in double quotes like in the above code. 如果要将其保留在ng-init ,则问题在于您的href='...'应该像上面的代码一样用双引号引起来。 In order to use double quotes, you need to escape them like this: \\" . You also misspelled href . 为了使用双引号,您需要像这样对它们进行转义: \\" 。您还拼错了href

From the AngularJS docs : 从AngularJS 文档中

"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." “唯一合适的ngInit用法是别名ngRepeat的特殊属性,如下面的演示所示。除此之外,您应该使用控制器而不是ngInit来初始化作用域上的值。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM