现在,之间的性能差异$broadcast和$emit已被淘汰,没有任何理由,更喜欢$scope.$emit到$rootScope.$broadcast?
他们是不同的,是的.
$emit 仅限于范围层次结构(向上) - 如果它符合您的设计,这可能是好的,但在我看来这是一个相当随意的限制.
$rootScope.$broadcast适用于所有选择收听活动的人,这在我看来是一个更明智的限制.
我错过了什么吗?
编辑:
为了澄清答案,发送的方向不是我追求的问题.$scope.$emit向上发送事件,$scope.$broadcast向下发送.但为什么不总是$rootScope.$broadcast用来接触所有预期的听众呢?
使用原始方式定义控制器,访问父级的范围相当简单,因为子范围原型继承自其父级.
app.controller("parentCtrl", function($scope){
$scope.name = "Parent";
})
.controller("childCtrl", function($scope){
$scope.childName = "child of " + $scope.name;
});
<div ng-controller="parentCtrl">
{{name}}
<div ng-controller="childCtrl">
{{childName}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
Controller-As方法似乎是声明控制器的推荐方法.但是使用Controller-As,上述方法不再适用.
当然,我可以pc.name从View中访问父作用域:
<div ng-controller="parentCtrl as pc">
{{pc.name}}
<div ng-controller="childCtrl as cc">
{{cc.childName}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我确实遇到了一些问题(潜在的意大利面条代码),但这个问题是关于从子控制器访问父作用域.
我能看到这个工作的唯一方法是:
app.controller("parentCtrl", function(){
this.name = "parent";
})
.controller("childCtrl", function($scope){
$scope.pc.name = "child of " + $scope.name;
// or
$scope.$parent.pc.name = "child of " + $scope.name;
// there's no $scope.name
// and no $scope.$parent.name
}); …Run Code Online (Sandbox Code Playgroud) SwiftUI 似乎有一个相当烦人的限制,这使得很难创建一个List或一段ForEach时间来绑定到每个元素以传递给子视图。
我见过的最常建议的方法是迭代索引,并获得与的绑定$arr[index](事实上,当他们删除的一致性时,Apple 提出了类似的建议):BindingCollection
@State var arr: [Bool] = [true, true, false]
var body: some View {
List(arr.indices, id: \.self) { index in
Toggle(isOn: self.$arr[index], label: { Text("\(idx)") } )
}
}
Run Code Online (Sandbox Code Playgroud)
这一直有效,直到数组的大小发生变化,然后它因索引超出范围运行时错误而崩溃。
这是一个会崩溃的例子:
class ViewModel: ObservableObject {
@Published var arr: [Bool] = [true, true, false]
init() {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.arr = []
}
}
}
struct ContentView: View {
@ObservedObject …Run Code Online (Sandbox Code Playgroud) 我熟悉以下方法来实现控制器之间的通信.
还有其他人吗?有更好的方法/最佳实践吗?
$broadcast/$emit.controller("Parent", function($scope){
$scope.$broadcast("SomethingHappened", {..});
$scope.$on("SomethingElseHappened", function(e, d){..});
})
.controller("Child", functions($scope){
$scope.$broadcast("SomethingElseHappened", {..});
$scope.$on("SomethingHappened", function(e, d){..});
})
.controller("AnotherChild", functions($scope){
$scope.$on("SomethingHappened", function(e, d){..});
});
Run Code Online (Sandbox Code Playgroud)
或者,从视图:
<button ng-click="$broadcast('SomethingHappened', data)">Do Something</button>
Run Code Online (Sandbox Code Playgroud)
好处:
缺点:
$rootScope,使用<div ng-controller="Parent">
<div ng-controller="Child">
<div ng-controller="ChildOfChild">
<button ng-click="someParentFunctionInScope()">Do</button>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
或者,在代码中
.controller("ChildOfChild", function($scope){
$scope.someParentFunctionInScope();
});
Run Code Online (Sandbox Code Playgroud)
好处:
缺点:
$rootScope,使用$watch控制器仅响应范围暴露数据的变化,从不调用函数.
.controller("Parent", function($scope){
$scope.VM = {a: "a", b: "b"};
$scope.$watch("VM.a", function(newVal, oldVal){
// react
}); …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的应用程序创建一个有点优雅的导航系统。下面是一个尝试返回 View 类型的函数。这不编译:
func getView(view: String) -> View {
switch view {
case "CreateUser":
return CreateNewsView()
default:
return nil
}
}
Run Code Online (Sandbox Code Playgroud)
以上导致编译错误: Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements
感谢您的帮助。
我有以下状态定义$stateProvider:
$stateProvider.state("byTeams", {url : "/team/{id}/{year}", ...})
$stateProvider.state("byPlayer", {url : "/player/{id}/{year}", ...})
Run Code Online (Sandbox Code Playgroud)
更改一年时,我希望URL与URL的{year}部分匹配,如果它与默认值匹配(比如2014).换句话说,当:
$state.go("byTeams", {year: 2014}) --> www.example.com/app/#/team/343
$state.go("byTeams", {year: 2013}) --> www.example.com/app/#/team/343/2013
Run Code Online (Sandbox Code Playgroud)
当我切换到一个byPlayer视图(假设年份是2014年 - 默认):
$state.go("byPlayer", {id: 555}) --> www.example.com/app/#/player/555/
Run Code Online (Sandbox Code Playgroud)
否则,URL将是: www.example.com/app/#/player/555/2013
我在将路由中的解析参数注入控制器时遇到问题.我正在为对象设置解析值{name: 'Banner', slug: 'banner'},但是我收到错误.
App.js
var app = angular.module('CMS', ['fields', 'ngRoute']);
app.controller('ModuleController', ['$http', 'properties',
function($http, properties) {
var module = this;
module.properties = properties;
if (module.properties.slug.length) {
$http.get(module.properties.slug + '.php').success(function(data) {
module.list = data;
});
}
}
]);
app.controller('HomeController', function() {});
app.config(function($routeProvider) {
$routeProvider
// route for the banner page
.when('/banner1', {
templateUrl: 'banner1.php',
controller: 'ModuleController',
resolve: {
properties: function() {
return { name: 'Banner', slug: 'banner' };
}
}
})
.when('/home', {
templateUrl: 'home.php',
controller: 'HomeController'
}) …Run Code Online (Sandbox Code Playgroud) 是否有一种标准方法可以将 a 绑定TextField到枚举的关联值?
所以,鉴于此:
enum Choice {
case one(String)
case two(String)
}
Run Code Online (Sandbox Code Playgroud)
我可以以某种方式将它用作视图中的绑定吗:
@State var choice: Choice = .one("")
var body: some View {
TextField("", text: $choice) // <- this clearly wouldn't work
}
Run Code Online (Sandbox Code Playgroud)
这样,如果choice设置为choice = .one(""),则 TextField 将更新 的关联值one。
编辑
澄清一下,是否可以只绑定每个的关联值,因此如果choice变量是.one,那么.one的关联值就会改变;与.two、 等相同。
Ng-repeat存在于表格行上
我的查询是如何实现以下目标:
<tr ng-repeat="x in y">
Looping here....
</tr>
Run Code Online (Sandbox Code Playgroud)
现在,当数据对象循环上<tr>.我有一个场景,我必须显示两行中的1行数据<tr>.
例如.
表
Data3数据3.2数据3.3数据3.4
Data4 data4.2 data4.3
即显示两行中的1行数据
不能使用ng-repeat-end
我正在尝试使用ng-blur <tr ng-blur="blurFn()">但它不会触发.它仅在子<input>元素上触发.
这不是特定的<tr>或<td>.根据Angular 文档,ng-blur对模糊事件做出反应,模糊事件不会"冒泡"."焦点"事件泡沫,但没有ng-focusout.
我错过了什么或者我是否需要创建一个新的指令来处理焦点事件?
这是代码片段和小提琴:
<div ng-app="App">
<div ng-controller="Ctrl as ctrl">
<table>
<tr ng-repeat="item in ctrl.items" ng-blur="ctrl.blurFn()">
<td><input ng-model="item.A"/></td>
<td><input ng-model="item.B"/></td>
</tr>
</table>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
angular.module("App", [])
.controller("Ctrl", function(){
this.blurFn = function($event){
console.log($event.target);
};
this.items = [
{A: "111", B: "222"},
{A: "111", B: "222"},
{A: "111", B: "222"},
{A: "111", B: "222"},
{A: "111", B: "222"}
];
});
Run Code Online (Sandbox Code Playgroud)