- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个使用该 View 中的父作用域的指令。该指令有一个使用隔离作用域的子指令。我试图让父指令观察对子指令的 ngModel 所做的任何更改,并在进行更改时更新其自己的模态。这是一个可能解释得更好的 jsfiddle:http://jsfiddle.net/Alien_time/CnDKN/
代码如下:
<div ng-app="app">
<div ng-controller="MyController">
<form name="someForm">
<div this-directive ng-model="theModel"></div>
</form>
</div>
</div>
Javascript:
var app = angular.module('app', []);
app.controller('MyController', function() {
});
app.directive('thisDirective', function($compile, $timeout) {
return {
scope: false,
link: function(scope, element, attrs) {
var ngModel = attrs.ngModel;
var htmlText = '<input type="text" ng-model="'+ ngModel + '" />' +
'<div child-directive ng-model="'+ ngModel + '"></div>';
$compile(htmlText)(scope, function(_element, _scope) {
element.replaceWith(_element);
});
// Not sure how to watch changes in childDirective's ngModel ???????
}, // end link
} // end return
});
app.directive('childDirective', function($compile, $timeout) {
return {
scope: {
ngModel: '='
},
link: function(scope, element, attrs, ngModel) {
var htmlText = '<input type="text" ng-model="ngModel" />';
$compile(htmlText)(scope, function(_element, _scope) {
element.replaceWith(_element);
});
// Here the directive text field updates after some server side process
scope.ngModel = scope.dbInsertId;
scope.$watch('dbInsertId', function(newValue, oldValue) {
if (newValue)
console.log("I see a data change!"); // Delete this later
scope.ngModel = scope.imageId;
}, true);
},
} // end return
});
在示例中,您可以看到在父指令及其子指令中都有一个文本输入。如果您在其中的每一个中键入,另一个模型就会更新,因为它们被 ngmodel
绑定(bind)。但是,子指令的文本输入在服务器连接后得到更新。发生这种情况时,父指令中的文本输入不会更新。所以我想我需要观察子指令中的 ngModel 是否有任何变化。 我该怎么做?有道理吗?
最佳答案
正如@shaunhusain 提到的,您必须使用 ngModelController 与 ngModel 进行交互。在 ngModelController 上,您可以在 $modelValue
上设置监视,并且可以通过调用 $setViewValue
更改模型中的值。请记住,要使用 ngModelController,您需要将 require: "ngModel"
添加到指令定义对象中。
当您从 Angular 外部(例如从您的数据库)获取一个值并且您又想使用该值来更改模型值时,您需要将该代码包装在 scope.$apply()
app.directive('thisDirective', function($compile, $timeout, $log) {
return {
scope: false,
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
...
scope.$watch(
function(){
return ngModel.$modelValue;
}, function(newValue, oldValue){
$log.info('in *thisDirective* model value changed...', newValue, oldValue);
}, true);
}, // end link
} // end return
});
app.directive('childDirective', function($compile, $timeout, $log) {
return {
scope: {
ngModel: '='
},
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
...
scope.$watch(
function(){
return ngModel.$modelValue;
}, function(newValue, oldValue){
$log.info('in *childDirective* model value changed...', newValue, oldValue);
}, true);
// make believe change by server
setTimeout(function() {
scope.$apply(function() {
ngModel.$setViewValue('set from the server...');
};
},5000);
},
} // end return
});
相关的jsfiddle http://jsfiddle.net/CnDKN/2/
但是 我不认为这不是 $setViewValue
的正确用法。根据文档,这应该用于更新 UI 上显示的值,不一定是模型值。
实际上还有另一种方法可以完成这项工作,我认为它更直接、更易于使用。只需使用 =attr
设置您要在 thisDirective
和 childDirective
中使用的属性的双向绑定(bind)。然后你可以在你的指令中设置 ng-model 属性设置,当你第一次使用它时你甚至不需要知道它在下面使用 ng-model。
这是向您展示我的意思的代码:
app.directive('thisDirective', function($compile, $timeout, $log) {
return {
scope: {
thisval: '='
},
link: function(scope, element, attrs) {
var htmlText = '<input type="text" ng-model="thisval" />' +
'<div child-directive childval="thisval"></div>';
$compile(htmlText)(scope, function(_element, _scope) {
element.replaceWith(_element);
});
scope.$watch('thisval',function(newVal,oldVal){
$log.info('in *thisDirective* thisval changed...',
newVal, oldVal);
});
}, // end link
} // end return
});
app.directive('childDirective', function($compile, $timeout, $log) {
return {
scope: {
childval: '='
},
link: function(scope, element, attrs) {
var htmlText = '<input type="text" ng-model="childval" />';
$compile(htmlText)(scope, function(_element, _scope) {
element.replaceWith(_element);
});
scope.$watch('childval',function(newVal,oldVal){
$log.info('in *childDirective* childval changed...',
newVal, oldVal);
});
// make believe change that gets called outside of angular
setTimeout(function() {
// need to wrap the setting of values in the scope
// inside of an $apply so that a digest cycle will be
// started and have all of the watches on the value called
scope.$apply(function(){
scope.childval = "set outside of angular...";
});
},5000);
},
} // end return
});
更新的 jsfiddle 示例:http://jsfiddle.net/CnDKN/3/
关于javascript - 如何观看指令的指令 ng-model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22819494/
我有一个不断增长的日志文件。我如何通过 Ruby 脚本查看和解析它? 脚本将解析写入文件的每个新行,并在新行包含字符串 'ERROR' 时将内容输出到屏幕 最佳答案 def watch_for(fil
我想尝试更新 modelValue 并更改日期选择器中的值 watch(range, (range) => { emit("update:modelValue", range); consol
谁能给我一个例子,说明如何在 ng-include 上观察发射事件?准确地说,我想在我的指令中观察 $includeContentLoaded 事件......这是我的 html:
经过一番努力,我能够使用谷歌驱动器对自己进行身份验证,获取文件列表,获取我想要的文件。现在希望能够捕获google文档数据中的变化,从而知道发生变化的行数和列数。 感觉我的答案就在这里https://
我构建了一个具有指向 promise 的属性的指令。当 promise 解决时,我想运行一个对数据执行某些操作的函数。像这样: scope.$watch('source', function (da
我正在尝试编写一个执行以下操作的 gulp 文件: 运行监视任务,在任何模板更改时重建 Handlebars 模板。 如果构建失败,它会通过notify()方法通知BrowserSync,这样如果出现
我有一个用例,我每天在不同的时刻都有新文件,比如每隔一小时或两小时,所以我需要查看文件夹中的目录,并在添加新文件时触发发送这些新文件的事件我在 NIFI 上的网络服务的路径,知道如何实现这个以及为此使
有人可以推荐一个 JAVA 开源库来监控 FTP 位置更改吗?我正在尝试监视 FTP 位置的更改,一旦检测到更改,更改的文件将被复制到 SVN 位置以进行提交。 最佳答案 Java 远程目录轮询器 (
我想知道是否可以在 Apple Watch Simulator 中模拟心跳,如果可以,如何实现。 谢谢! 最佳答案 是的,您需要使用 HKWorkoutSession 关于xcode - 观看 OS
我目前正在使用 google calendar api 并尝试在 javascript 中编写一个函数,该函数使用 calendar.events.watch 函数以便在用户日历中有更新时进行更新 (
我需要监视网络应用程序中两个目录中的更改(上传的文件)。我创建了一个ServletContextListener来触发对这两个目录的监控。 我的问题是,当第一次监控开始时,线程被阻塞,第二次监控没有启
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improv
我正在开发一个应用程序,当 watch 被激活时,我需要启动一个计时器(使用 NSTimer)。使用计时器,我向 iPhone 询问一些信息(大约每 1 秒一次,最多 5 秒一次)。我用它来启动计时器
我只想向我的 WKInterfaceController 添加图像,但是... Xcode 告诉我: Unable to find image named "circle44" on Watch @I
除了 root_path 之外的所有路由都在产品中中断,即使我的 assert_select 测试所有 GREEN。 The core of the problem was misconfigured
我试图观察用户何时在 IScroll (v5) 中滚动。 我对 angular 还是很陌生,只是在学习编写指令。 从其他示例中,我正在尝试。 我的指令是 app.directive('watchScr
我有一个带有 Controller 的选择输入,当选择一个值时,希望将该值传递给将显示它的指令。 (在本例中,在传单 map 上绘制一个形状)。 问题是我希望该值是“即发即忘” - 因此他们选择形状,
我正在使用 Gulp 来编译和缩小我的 SASS。这工作正常,但我想通过使用 BrowserSync 来扩展自动化。 我已按照一些教程网站上的说明进行操作,但无法使其正常工作 - 当我更新 .scss
一段时间以来,我一直在尝试远离 FTP。 在掌握了 GIT 之后(我错过了什么!),将提交推送到 GitHub,然后最终将更改 pull 到我的远程服务器上并 pull 任何更改)。 我一直在看的一个
我有一个使用 Parse.com 的现有 iOS 应用程序。您可以使用用户名和密码、Facebook 或 twitter 登录,也可以创建新的登录名。我正在准备更新,它有一个 WatchKit 扩展,
我是一名优秀的程序员,十分优秀!