- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 $scope.$watch
时遇到问题关于我项目范围内的一些 Angular 变量。我做了一个样本jsfiddle来说明问题。
基本上我可以$scope.$watch
ng-switch
内的任何型号只要它是一个物体。如果它是一个字符串,它将不会触发监视表达式,并且不会在 ng-switch
的范围之外进行更改。区域。 Angular 是否复制原始字符串而不是像对象一样通过引用传递它?
我正在使用 ng-repeat
在一些元素上——这是我的一些代码:
<div ng-switch="key">
<div ng-switch-when="deal_id">
DEALID: <input type="text" ng-model="dealIdModel"/> -- {{dealIdModel}}
</div>
<div ng-switch-when="thing_id">
THING: <input type="text" ng-model="thingIdModel.test"/> -- {{thingIdModel.test}}
</div>
<div ng-switch-default>
DEFAULT: <input type="text" placeholder="email" ng-model="items.params.email"/> -- {{items.params.email}}
</div>
</div>
还有 JS:
$scope.items = {
"statusCode":"ACTIVE",
"params":{
"deal_id":"",
"thing_id":"",
"email":"Change me! I will get called"
}
};
$scope.dealIdModel = "I won't change outside of the loop or get called.";
$scope.thingIdModel = {test:'Change me! So will I!'};
$scope.$watch('items.params.email', function (now, then, scope) {
console.log('email change', now, then);
});
$scope.$watch('thingIdModel.test', function(now, then, scope) {
console.log('changed the thing', now, then);
});
$scope.$watch('dealIdModel', function(now, then, scope) {
console.log('dealID changed:', now, then);
});
最佳答案
这与 ng-repeat
创建的子作用域有关。由于创建了一个新作用域,如果您在子作用域上设置一个普通变量,您将更改那里的引用,但不会更改父作用域中的引用。当您使用对象时,父级会维护对该对象的引用,并且只有内部部分会发生变化。
这与您听到“始终在您的 ng-model 中加点”时遇到的问题相同
这里有更多详细信息: https://github.com/angular/angular.js/wiki/Understanding-Scopes
关于angularjs - ngModel 与 ngSwitch,无法监视字符串变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29682210/
我目前理解 [(ngModel)]="expression" 是从组件到 View 的双向绑定(bind),反之亦然。我也明白 [ngModel]="expression" 是单向绑定(bind)(我
我实际上是在重复一个 mat 输入字段,然后我用数组中对象的属性名称编写了 [(ngModel)] 作为 [(ngModel)] 的值 例如这是我拥有的对象数组: test = [{name: 'Ha
我正在观看的 Udemy Angular 6 视频显示使用第一种语法。但是,我使用 WebStorm 作为我的 IDE,当我使用 [(ngModel)]="variable" 语法时,它不会提供任何类
我正在尝试用 Angular 数组绘制 radioBoxes,然后获取选中的 radio 的值,但是模型不会改变它的值吗,有人可以帮我吗? HTML部分
我有以下 ckEditor 指令。底部是我从有关如何在编辑器中设置数据的示例中看到的两种变体: app.directive('ckEditor', [function () { return
这是一个模板示例: 这里他们两个做同样的事情。首选哪一个?为什么? 最佳答案 [(ngModel)]="overRideRate" 是 [ngModel]="overRideRate"的缩写形式
我得到的是一个动态生成的 ngModel 对象,并且某些不在顶层的属性仅在一个方向上绑定(bind)。 包括一个应该让事情变得更清晰的 plunkr,但我想做的是生成一个动态表单,并且如果属性嵌套在根
嗨,哪个更好?有什么区别?有什么优点和缺点? 这是两者的对比代码: 范围:{ ngModel:'=' } app = angular.module('app', []); a
我正在尝试根据 angularjs 中的某些属性绑定(bind)未知的数组索引。 //javascript method inside controller functio
我试图在 ngFor 循环中输入具有正则表达式要求的输入,但收到“无法读取未定义的属性‘有效’”错误,我将其用作页面加载时弹出的错误消息。 这是我目前的代码: (我拥有的键管道是一个自定义管道,因为
我正在尝试将 ngControl 表单验证添加到 Angualr js 2 中的 bootstrap 4 typehead 控件。代码如下。 {{ requ
[(ngModel)] 在表单标签内不工作 当我使用 Multi Select Outside Form 标签时,可以正常选择 All 和取消选择所有功能 但是当我把它放入 Form 里面时,它正在工
假设我想要一个指令,它接受用户在输入中键入的内容并将其转换为小写。 所以我想出了一个这样的指令(在这个 Plunker http://plnkr.co/edit/jnE3s8MRr1tFCX0WVYe
我的名为 datePicker 的指令位于输入元素上,它从模型中获取公历日期并将其转换为最终用户的波斯日期。 从 datePicker 更改日期(这里是 jalali(波斯日期选择器)),转换为 gr
在这个例子中,我看到了解释,但我仍然不确定我为什么在这种情况下将#name 模板变量设置为“ngModel”。如果有 2 或 3 个其他带有模板变量的输入字段,您是否也将它们的值设置为“ngModel
这是我的 HTML 代码: Start 因此,startDate 存储为数字(长整型),但我想显示本地日期时间。我真的不知道该怎么做。我使用相同的组件来创建和更新模型。创建工作正常,因为
喜欢在 this question ,我要加.error在表单域的父级 .control-group当scope.$invalid是真的。 但是,像 ng-class="{ error: formNa
我正在尝试为颜色选择器创建指令,但 ngModel 值的值未更新。知道出了什么问题吗? 这是我的代码: .directive('colpkr', [function () {
我不是 Angular 事件方面的专家,但这是我的问题。如果我运行以下代码,我基本上会用错误的信息更新后端,因为 item.their_platform 在 togglePlatform() 触发之前
我有这个数组试图在我的模板中显示,不知何故它无法正确显示。如果我这样做 [value],它将给我正确的值,但 ofc。不绑定(bind)到数组和值。 大批: {"pluNo":1,"pluName":
我是一名优秀的程序员,十分优秀!