Asp.net 学习AngularJs:ng模型不绑定到视图中
我对angularJS非常陌生,你可以说这是我使用angularJS的第一天 这看起来很傻,但我正在尝试做一些基本的东西,但不知怎么的,这些东西是不起作用的 我有一个文本框,如果您输入1234,Asp.net 学习AngularJs:ng模型不绑定到视图中,asp.net,angularjs,Asp.net,Angularjs,我对angularJS非常陌生,你可以说这是我使用angularJS的第一天 这看起来很傻,但我正在尝试做一些基本的东西,但不知怎么的,这些东西是不起作用的 我有一个文本框,如果您输入1234,Count应该是555,或者如果您输入任何数字,它应该是550,我将1234放在页面加载中,这样它会显示我555,但当我更改文本框中的值时,Count不会更改 <div ng-app> <div ng-controller="prCtrl">
Count
应该是555
,或者如果您输入任何数字,它应该是550
,我将1234
放在页面加载中,这样它会显示我555
,但当我更改文本框中的值时,Count
不会更改
<div ng-app>
<div ng-controller="prCtrl">
Enter Product ID to get the reviews details
<input type="number" ng-model="productId" required />
<br />
Total Review Count = {{ Count }}
</div>
</div>
function prCtrl($scope,$http) {
$scope.productId = 1234;
if ($scope.productId === 1234) {
$scope.Count = 555;
} else {
$scope.Count = 550;
}
}
输入产品ID以获取评论详细信息
总审核计数={Count}
函数prCtrl($scope$http){
$scope.productId=1234;
如果($scope.productId==1234){
$scope.Count=555;
}否则{
$scope.Count=550;
}
}
如何根据文本框中输入的值更改{{Count}}
谢谢一个选项是订阅模型更改并在那里执行您的逻辑: 控制器:
function prCtrl($scope,$http) {
$scope.productId = 1234;
$scope.$watch('productId', function(newValue, oldValue){
if (newValue === 1234) {
$scope.Count = 555;
} else {
$scope.Count = 550;
}
});
}
视图:
输入产品ID以获取评论详细信息
总审核计数={Count}
我已经测试过了,它似乎做了你想要的
还有最后一点——你提到你是AngularJS的新手——我强烈推荐egghead.io在AngularJS()上的会话。他们擅长让您了解AngularJS:)或者您可以使用函数,而不用使用$watch查看值
function prCtrl($scope,$http) {
$scope.productId = 1234;
$scope.getCount = function() {
if ($scope.productId === 1234) {
return 555;
} else {
return 550;
}
}
}
视图:
<div ng-app>
<div ng-controller="prCtrl">
Enter Product ID to get the reviews details
<input type="number" ng-model="productId" required />
<br />
Total Review Count = {{ getCount() }} // replaced with function call
</div>
</div>
输入产品ID以获取评论详细信息
总审核计数={{getCount()}}//替换为函数调用
当范围内的模型发生更改时,将调用此函数,因此它将始终更新您的值抱歉,我没有得到您的支持。可能是我解释得不好。让我编辑我的question@patel.milanb没问题,很高兴我能帮忙:)
<div ng-app>
<div ng-controller="prCtrl">
Enter Product ID to get the reviews details
<input type="number" ng-model="productId" required />
<br />
Total Review Count = {{ getCount() }} // replaced with function call
</div>
</div>