是否有任何方法可以在角度完成所有观察周期后调整角度自定义功能.
要求 我的控制器内有多个监视功能.现在我想在只有角度执行所有手表功能后执行一个功能
是否有内置的方法来阻止$broadcast事件沿着范围链走下去?
事件传递的事件对象$broadcast没有stopPropagation方法(就像$ rootScope提到的文档一样.)但是这个合并的pull请求表明$broadcast事件可以stopPropagation调用它们.
我可能会完全倒退,但我正在尝试制作三个嵌套指令,让我们调用它们:屏幕,组件和小部件.我希望widget能够触发组件中的某些行为,从而触发屏幕中的某些行为.所以:
.directive('screen', function() {
return {
scope: true,
controller: function() {
this.doSomethingScreeny = function() {
alert("screeny!");
}
}
}
})
.directive('component', function() {
return {
scope: true,
controller: function() {
this.componentFunction = function() {
WHAT.doSomethingScreeny();
}
}
}
})
.directive('widget', function() {
return {
scope: true,
require: "^component",
link: function(scope, element, attrs, componentCtrl) {
scope.widgetIt = function() {
componentCtrl.componentFunction();
};
}
}
})
<div screen>
<div component>
<div widget>
<button ng-click="widgetIt()">Woo Hoo</button>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我可以在小部件的链接fn中使用父组件require: "^component",但是如何进一步让组件控制器访问其包含的屏幕?
我需要的是组件中的内容,因此当您单击小部件的按钮时,它会提醒"screeny!". …
我已经在范围上定义了一个函数.当我从{{}}内的视图中调用它时,它会执行X次.
调节器
function testCtrl($scope) {
$scope.myFunc = function(name) {
return "Hello " + name;
}
}
Run Code Online (Sandbox Code Playgroud)
HTML
<div>{{myFunc('Joe')}}</div>
Run Code Online (Sandbox Code Playgroud)
您可以在此示例中看到它:http://jsfiddle.net/rbRvD/2/
或者使用Plunker:http://plnkr.co/edit/LLQ7cKs2fEoBwv0C5XPE
我猜这是以错误的方式完成的,但为什么它会执行这么多次?
我正在编写一个需要隔离范围的指令,但我想通过ngModel将它绑定到父作用域.
这里的问题是父级的范围值没有改变.
标记
<form name="myForm" ng-app="customControl">
<div ng-init="form.userContent"></div>
<div contenteditable name="myWidget" ng-model="form.userContent" required>Change me!</div>
<span ng-show="myForm.myWidget.$error.required">Required!</span>
<hr />
<textarea ng-model="form.userContent"></textarea>
</form>
Run Code Online (Sandbox Code Playgroud)
JS
angular.module('customControl', []).directive('contenteditable', function() {
return {
restrict : 'A', // only activate on element attribute
require : '?ngModel', // get a hold of NgModelController
scope: {},
link : function(scope, element, attrs, ngModel) {
if (!ngModel)
return; // do nothing if no ng-model
// Specify how UI should be updated
ngModel.$render = function() {
element.html(ngModel.$viewValue || ''); …Run Code Online (Sandbox Code Playgroud) 我想访问父指令的范围,但我似乎无法获得正确的设置组合.这是可能的,这是正确的方法吗?
我真的想避免在MyCtrl中放置类似SOME_CONST(这可以帮助我通过控制流程进行DOM更新)
<div ng-controller="MyCtrl">
<parent>
<child></child>
</parent>
</div>
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.obj = {prop:'foo'};
}
myApp.directive('parent', function() {
return {
scope: true,
transclude: true,
restrict: 'EA',
template: '<div ng-transclude><h1>I\'m parent {{obj.prop}}<h1></div>',
link: function(scope, elem, attrs) {
scope.SOME_CONST = 'someConst';
}
}
});
myApp.directive('child', function() {
return {
restrict: 'EA',
template: '<h1>I\'m child.... I want to access my parent\'s stuff, but I can\'t. I can access MyCtrlScope though, see <b>{{obj.prop}}</b></h1> how can I access the <b>SOME_CONST</b> value …Run Code Online (Sandbox Code Playgroud) 我正在写一个angularJS应用程序.在这个特定的控制器中,我通过该$window.open服务打开一个新的浏览器窗口.但是在新窗口中,所有$scope变量都会丢失.
我尝试使用,window.parent但这不起作用.事实上,在新的浏览器窗口中,所有应用程序服务,控制器或作用域根本不起作用,这是真的吗?有没有办法打开一个新的浏览器窗口,但仍然使新窗口属于旧窗口中相同的角度应用程序?我需要访问一些angularJS服务和打开该新窗口的控制器的范围.这有可能吗?
javascript angularjs angularjs-service angularjs-scope angularjs-controller
我明白我可以按元素获得范围:
scope = angular.element($0).scope();
scope.$id; // "003"
Run Code Online (Sandbox Code Playgroud)
如何反转:使用范围查找DOM元素$id,例如003?
我想这样做是为了调试目的.我的范围树显示了一些东西,我想确定它来自哪里.
我有这个HTML:
<p>Hello {{name}}</p>
Run Code Online (Sandbox Code Playgroud)
控制器是:
function myCtrl(scope, service) {
scope.name = service.getUsername(); // service.getUsername() return "World!"
}
myCtrl.$inject = ['$scope', 'originalService'];
Run Code Online (Sandbox Code Playgroud)
该服务工作正常,所以我不粘贴代码...在这种情况下,结果是" Hello world! "我以这种方式更改了HTML:
<p>Hello {{service.getUsername()}}</p>
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
我改变了控制器:
function myCtrl(scope, service) {
scope.ser = service;
}
myCtrl.$inject = ['$scope', 'originalService'];
Run Code Online (Sandbox Code Playgroud)
然后是HTML
<p>Hello {{ser.getUsername();}}</p>
Run Code Online (Sandbox Code Playgroud)
这有效!
所以我的问题是:
这是直接在HTML中使用服务功能的唯一方法,还是我遗漏了什么?
我基本上希望在Backbone的集合中绑定到'add'和'remove'事件.我认为在AngularJS中基本上没办法做到这一点,我们已经解决的当前解决方法是$watch()使用数组length并手动区分/重新计算整个事物.这真的是很酷的孩子吗?
编辑:具体来说,观察数组的长度意味着我不知道哪个元素已被更改,我需要手动"diff".