问题描述
在后端提供了一个完全渲染现场和前端我要为angularjs处理通过Ajax的呼叫/数据绑定的动态内容,但如果你传递指令NG绑定,然后angularjs直接绑定他们这是他们的初始值NULL用户的任何行动之前。
我找到了一个解决方案,哈克,但我想知道是否有一个更好的或者其他的js框架,不正是我想要做的:
https://github.com/herschel666/angular-lazy-bind
下面的例子应该有助于理解我的问题...
一旦JS加载inital值HOLA服务器端(送到服务器端)已经一去不复返了。
我想要的innerHTML /值保持这样的,保持绑定活跃,但懒惰,以便它只是一个动作后更改
重要的一点是angularjs不重写哪些服务器端已经writen(redered)
< HTML NG-应用=对myApp>
< HEAD>
<间的charset =UTF-8>
<标题>将文档和LT的称号; /标题>
< /头><身体GT;
< DIV NG控制器=GreetingController>
<! - 这个值已经留...但保留其结合特性,以改变它的AFER用户动作 - >
<跨度NG绑定=问候> HOLA服务器端< / SPAN>
<按钮NG点击=更新()>更新< /按钮>
< / DIV>
< /身体GT;
&LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js\"></script>
&LT;脚本类型=文/ JavaScript的&GT;
VAR对myApp = angular.module('对myApp',[]);myApp.controller('GreetingController',['$范围',函数($范围){
$ scope.update =功能(){
//做Ajax调用和设置的问候和其他数据绑定
$ scope.greeting ='霍拉!;
}
}]);
&LT; / SCRIPT&GT;
&LT; / HTML&GT;
同构
You are trying to build an isomorphic application. React will allow you to create isomorphic applications, but normally requires the backend be built in node.js. There is a way to use React with PHP. There are other solutions to isomorphism as well.
binding
You can feed angular the server-side data by passing it in the HTML with a script
tag, using the json_encode
PHP function. This way angular will have the correct data when it starts up.
<script>
angular.module('app').constant('applicationData', <?= json_encode(application_data) ?>);
</script>
Then you can inject applicationData
and use it to initialize your directive. This approach is less than ideal because it forces you to deal with the same data twice. That's why using a view library like React which was built to support isomorphism is probably a better choice when creating an isomorphic application.
这篇关于Angularjs:服务器端(PHP)的渲染和数据的事件后,结合客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!