如何在rails中初始化Angularjs ng-model

 歪歪Doris 发布于 2023-02-13 17:21

关于使用angularjs双向绑定和rails erb文件的问题.

假设我的.erb文件中的输入值具有原始值,即

example.erb


正如您在上面的示例中所注意到的那样,输入也与angularJs模型绑定.example.js

mayApp.controller('newItemController', function itemController($scope) { 
   $scope.item = {title:"angularJs model value", price: 1000}
}

我发现原始的@ item.title被angularJs模型值覆盖.但是,我希望事情以相反的方式发生,也就是说,角度js模型是通过.erb文件中的值初始化的.我怎样才能做到这一点?

我试图将example.js放入资产管道.即example.js.erb

mayApp.controller('newItemController', function itemController($scope) { 
   $scope.item = {title:"<%= @item.title %>", price: 1000}
}

但是@item总是在管道中.我想@item只在视图中可用?

1 个回答
  • 你可以使用ng-init这个:

    <input type="text" ng-init="item.title = '<%= @item.title %>'" ng-model ="item.title">
    

    2023-02-13 17:23 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有