Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS:从HTML中的数据初始化模型?_Angularjs_Angularjs Scope - Fatal编程技术网

AngularJS:从HTML中的数据初始化模型?

AngularJS:从HTML中的数据初始化模型?,angularjs,angularjs-scope,Angularjs,Angularjs Scope,我正在考虑尝试从HTML中的数据初始化AngularJS模型(部分),而不是请求服务器以JSON的形式获取数据,或者直接在页面中嵌入JSON对象 (服务器目前只以HTML(而不是JSON)呈现数据,我希望避免现在重写“太多”代码,HTML与搜索引擎配合得很好。 应用程序本身就是一个讨论系统。) 下面是我的HTML(简化)示例。其中嵌入的数据是一个帖子ID(123456)、一个作者ID(789)、一个作者姓名(Kitty overlord)和一条短信(“Kittes Cats!只是开玩笑而已”)

我正在考虑尝试从HTML中的数据初始化AngularJS模型(部分),而不是请求服务器以JSON的形式获取数据,或者直接在页面中嵌入JSON对象

(服务器目前只以HTML(而不是JSON)呈现数据,我希望避免现在重写“太多”代码,HTML与搜索引擎配合得很好。 应用程序本身就是一个讨论系统。)

下面是我的HTML(简化)示例。其中嵌入的数据是一个帖子ID(123456)、一个作者ID(789)、一个作者姓名(Kitty overlord)和一条短信(“Kittes Cats!只是开玩笑而已”)

也许我可以这样使用
ng init

<div id="post-123456"
     ng-controller="CommentCtrl"
     ng-init="{ postId =..., authorId =..., text =... }">      <--- look here
  <div class="dw-p-hd">
    By <span data-dw-u-id="789">Kitty overlord</span>
  </div>
  <div class="dw-p-bd">
    <p>Kittens...</p>
  </div>
</div>

似乎最好将所有这些数据存储在json中,如:

   //...
   posts: [
      { postId : 123456,
        authorId : 789,
        text : 'Kittens ....' },
      { postId : 123457,
        authorId : 790,
        text : 'Puppies....' }
   ]
   //...
然后用
ng repeat
填充它,如:

  <div ng-repeat="post in posts" id="{{post.postId}}"
     ng-controller="CommentCtrl" >     
     <div class="dw-p-hd">
       By <span data-dw-u-id="{{post.authorId}}">Kitty overlord</span>
     </div>
     <div class="dw-p-bd">
       <p>{{post.text}}</p>
     </div>
  </div>

基蒂·奥克洛德
{{post.text}


我也有类似的问题,可能对您有用:

谢谢您的回答。您所描述的实际上就是我所说的“在页面中直接嵌入JSON对象”+1因为这似乎是每个人都在做的事情,所以也许这真的是最好的方法。因为有一个有用的链接指向你自己的问题我想我实际上认识你问题的标题,但我认为它是关于其他事情,即关于将数据插入DOM,而不是从DOM中“提取”数据。如果有帮助,我很高兴!(是的,我的英语还远远不够完美,所以有些细微差别可能不清楚,对不起):-)
   //...
   posts: [
      { postId : 123456,
        authorId : 789,
        text : 'Kittens ....' },
      { postId : 123457,
        authorId : 790,
        text : 'Puppies....' }
   ]
   //...
  <div ng-repeat="post in posts" id="{{post.postId}}"
     ng-controller="CommentCtrl" >     
     <div class="dw-p-hd">
       By <span data-dw-u-id="{{post.authorId}}">Kitty overlord</span>
     </div>
     <div class="dw-p-bd">
       <p>{{post.text}}</p>
     </div>
  </div>