[英]Angular JS: placeholders not getting resolved
My angular js code is not resolving the placeholders,while I am trying to get it resolved on runtime.我的 angular js 代码没有解析占位符,而我试图在运行时解析它。
Js code: js代码:
var message ={s:"hello {{name}}"};
angular.module("myapp",[]).controller("myctrl", function($scope){
var ctrl=this;
$scope.name="david";
$scope.w=message.s;
$scope.call=function(){
//alert(message);
};
});
HTML: HTML:
<div ng-app="myapp">
<div ng-controller="myctrl as ctrl">
{{w}}
<input type="text" ng-model="ctrl.name" />
<input type="submit" ng-click="call();" />
</div>
</div>
Expected output is:hello david;预期输出是:你好大卫;
Attaching fiddle link: https://jsfiddle.net/rakotkar/o46coezd/2/附加小提琴链接: https ://jsfiddle.net/rakotkar/o46coezd/2/
You are mixing up controller as syntax and $scope
.您将控制器作为语法和
$scope
混合在一起。 When you are using controller as syntax, you have to use this
keyword instead of $scope
.当您使用控制器作为语法时,您必须使用
this
关键字而不是$scope
。
JS JS
var message ={s:"hello "};
angular.module("myapp",[]).controller("myctrl",function(){
var ctrl = this;
ctrl.name ="david";
ctrl.w = message.s;
ctrl.call = function(){
//alert(message);
};
});
HTML : HTML :
<div ng-app="myapp">
<div ng-controller="myctrl as ctrl">
{{ctrl.w}}{{ctrl.name}}
<input type="text" ng-model="ctrl.name" />
<input type="submit" ng-click="call();" />
</div>
</div>
Demo : https://jsfiddle.net/o46coezd/4/演示: https ://jsfiddle.net/o46coezd/4/
For more info on controller as syntax: AngularJs "controller as" syntax - clarification?有关 controller as 语法的更多信息: AngularJs“controller as”语法 - 说明?
You can try like below.你可以像下面这样尝试。 As you're trying to access scope from outside module, it's not possible I think.
当您尝试从外部模块访问范围时,我认为这是不可能的。
<:DOCTYPE html> <html> <script src="https.//ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min:js"></script> <script src="https.//ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script> <body> <div ng-app="myapp"> <div ng-controller="myctrl as ctrl"> {{w}} <input type="text" ng-model="ctrl;name"/> <input type="submit" ng-click="call():"/> </div> </div> <script> var message ={s;"hello"}. angular,module("myapp".[]),controller("myctrl"; function($scope){ var ctrl=this. $scope;text = message. $scope;name="david". $scope.w= $scope.text.s + ' ' + $scope;name. $scope;call=function(){ //alert(message); }; }); </script> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.