使用$scope它很容易发出事件或观看一个事件.
(function() {
"use strict";
angular
.module("app")
.controller("Ctrl", [
"$scope",
CtrlDefinition
]);
function CtrlDefinition($scope) {
$scope.$on("change", listenForChange);
function listenForChange(data) {
//do something
}
}
})();
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试使用var vm = this语法,我警告说$on,$emit和$broadcast不是的方法this.我怎样才能访问它们?我还需要注入$scope控制器定义吗?
(function() {
"use strict";
angular
.module("app")
.controller("Ctrl", CtrlDefinition);
function CtrlDefinition() {
var vm = this;
vm.$on("change", listenForChange); //$on is not a method of vm
}
})();
Run Code Online (Sandbox Code Playgroud)
你可以做这样的事情,但它不会打败不必使用的目的$scope吗?
(function() {
"use strict";
angular
.module("app")
.controller("Ctrl", [ …Run Code Online (Sandbox Code Playgroud) 我使用1.5组件,但我认为这并不重要.
我正在尝试=在父控制器和子指令隔离范围之间进行单个绑定.子隔离范围是字面上插入绑定; 而不是vm.data内插到我在控制器中定义的数据,它实际上vm.data是一个字符串.
如果我尝试单向绑定@然后再次,"插值"值将导致{{vm.data}}字面上的结果.
如何将父控制器中定义的字符串放入子组件的模板中?
angular
.module('app', [])
.controller('Ctrl', function () {
this.str = '<0>, blah, <1>';
})
.component('appComponent', {
restrict: 'E',
controller: 'Ctrl as vm',
template: '<div><app-str appstr-data="{{vm.str}}"></app-str></div>'
})
.component('appStr', {
bindings: { appstrData: '@' },
restrict: 'EA',
template: function($element, $attrs) {
console.log($attrs.appstrData)
return '<span>'+$attrs.appstrData+'</span>';
}
});
Run Code Online (Sandbox Code Playgroud)
我想在 $ resource构造url 之前以编程方式更改路由参数.我无法使用angular的http拦截器来执行此操作,因为此时路由已经连接在一起.
给出一个Assortment.model.js
module.exports = function($resource) {
return $resource("", {}, {
get: {
url: "/assortment/:model/:id",
method: "GET",
params: {id: "@id", model: "@model"} //< this needs to be uppercase
}
});
};
Run Code Online (Sandbox Code Playgroud)
......和一些controller.js
["Supplier", function(Supplier) {
Supplier.Assortment.get({ id: 5, model: "user" })
}]
Run Code Online (Sandbox Code Playgroud)
如何强制执行始终转换{model: "user"}为的挂钩{model: "User"}
javascript angularjs angularjs-resource ngresource angularjs-http
我在 NextJS 应用程序上使用 mobx-state-tree 进行服务器端渲染。仅在服务器上,我获取 API 来加载一些信息并添加到我的商店。但是在客户端加载之后,我的存储被清除,因为我需要在服务器和客户端上实例化。这是我的问题。
我的商店:
const Store = types
.model('Store', {
user: types.compose(User),
});
const createStore = Store.create({
user: {},
});
let store = null;
/**
* Init store
*
* @param {bool} isServer
* @param {object} snapshot
*/
const initStore = (isServer, snapshot = null) => {
if (isServer) {
store = createStore;
}
if (store === null) {
store = createStore;
}
if (snapshot) {
applySnapshot(store, snapshot);
}
return store;
};
Run Code Online (Sandbox Code Playgroud)
我在客户端的组件:
static getInitialProps({ req …Run Code Online (Sandbox Code Playgroud) 我正在制作一个chrome扩展程序,它包含多个应用程序(多个devtools面板),每个应用程序都是独立的.我需要webpack来观察多个入口点并生成多个bundle,我不需要常见的块.我绝对不希望在客户端代码中使用AMD 这样.
/appOne
index.js
/components
/blah
/foobar
/appTwo
index.js
/components
..etc
/appThree
index.js
/components
..etc.
/chrome <- basically the "dist" folder
/appOne
index.html
bundle.js
/appTwo
index.html
bundle.js
/appThree
etc...
Run Code Online (Sandbox Code Playgroud)
根据我一直在做的多个条目的文档:
{
entry: {
appOne: './appOne/index.js',
appTwo: './appTwo/index.js'
},
output: {
path: path.join(__dirname, 'chrome', '[name]'),
filename: 'bundle.js' //can be the same since they should be output in different folders
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Path variable [name] not implemented in this context: C:\Users\Admin\projects\crx\chrome\[name]
Run Code Online (Sandbox Code Playgroud)
所以我猜你不能[name]在path多个条目的设置中有变量?
我正在尝试以编程方式更改扩展 devtools 的 chrome 扩展中的 pdf 预览页面。
显现
"content_security_policy": "img-src 'self' data; script-src 'self'; object-src 'self'; data-uri 'self'"
Run Code Online (Sandbox Code Playgroud)
当我将src属性设置为 iframe 时,我可以成功加载 pdf 并且它将动态生成。
<iframe src="data:application/pdf;base64,..."></iframe>
Run Code Online (Sandbox Code Playgroud)
但是,当我对embed或objecthtml 元素尝试相同时,我得到:
<embed src="data:application/pdf;base64,...">
<object data="data:application/pdf;base64,..."></object>
Run Code Online (Sandbox Code Playgroud)
拒绝从 'data:application/pdf;base64,{{data}}' 加载插件数据,因为它违反了以下内容安全策略指令:“object-src 'self'”。
为什么?重置srciframe 上的属性是将焦点放在嵌套的内容窗口上,因此当用户在父窗口中输入时突然textarea变得模糊(这真的很烦人)。我认为使用embedorobject元素可以缓解嵌套文档问题。
为了使嵌入正常工作,正确的 csp 语法是什么?我正在直接查看w3文档,它并没有真正的帮助。例如,我在清单中尝试了以下语法:
"content_security_policy": "object-src 'self' data"
Run Code Online (Sandbox Code Playgroud)
...当您尝试刷新chrome://extensions.
javascript iframe google-chrome-extension content-security-policy
说我想要touch六个文件
one.html
one.css
two.html
two.css
three.html
three.css
Run Code Online (Sandbox Code Playgroud)
我xargs该如何使用?我正在查看手册页,但我不确定获取 stdin 管道的语法。
$ echo one two three | xargs -n 1 touch $1.html $1.css // nope
Run Code Online (Sandbox Code Playgroud) 我的项目是这样的:
views/
layout.ejs
data/
US-states.json
public/
index.html
Run Code Online (Sandbox Code Playgroud)
US-states.json 看起来像这样
{
"DC": "District of Colombia",
"NY": "New York",
etc...
}
Run Code Online (Sandbox Code Playgroud)
如何将这个 json 数据拉入layout.ejs以在这样的循环中呈现一些 div?
<div>DC: District of Colombia</div>
<div>NY: New York</div>
Run Code Online (Sandbox Code Playgroud)
我试过这样的事情,但我在谷歌搜索解决方案时遇到了麻烦:
var data = JSON.parse('./../data/US-states.json')
<% for(var p in data) {%>
<div>p: data[p]</div>
<% } %>
Run Code Online (Sandbox Code Playgroud)