我是Angular的新手并试图找出如何做事......
使用AngularJS,如何注入要在另一个控制器中使用的控制器?
我有以下代码段:
var app = angular.module("testApp", ['']);
app.controller('TestCtrl1', ['$scope', function ($scope) {
$scope.myMethod = function () {
console.log("TestCtrl1 - myMethod");
}
}]);
app.controller('TestCtrl2', ['$scope', 'TestCtrl1', function ($scope, TestCtrl1) {
TestCtrl1.myMethod();
}]);
Run Code Online (Sandbox Code Playgroud)
当我执行此操作时,我收到错误:
Error: [$injector:unpr] Unknown provider: TestCtrl1Provider <- TestCtrl1
http://errors.angularjs.org/1.2.21/$injector/unpr?p0=TestCtrl1Provider%20%3C-%20TestCtrl1
Run Code Online (Sandbox Code Playgroud)
我是否应该尝试在另一个控制器内使用控制器,还是应该将其作为服务?
我一直在寻找一个小时试图弄清楚为什么这不起作用.
我有一个带有WebAPI的ASP.Net MVC 5应用程序.我正在尝试获取Request.GetOwinContext().身份验证,但我似乎无法找到如何包含GetOwinContext.这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using TaskPro.Models;
namespace TaskPro.Controllers.api
{
public class AccountController : ApiController
{
[HttpPost]
[AllowAnonymous]
public ReturnStatus Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var ctx = Request.GetOwinContext(); // <-- Can't find this
return ReturnStatus.ReturnStatusSuccess();
}
return base.ReturnStatusErrorsFromModelState(ModelState);
}
}
}
Run Code Online (Sandbox Code Playgroud)
从我读过的内容来看,它应该是System.Net.Http的一部分,但我已经包含了它,但它仍然没有解决.Ctrl-Space也没有给我任何intellisense选项.
我在这里错过了什么?
我是Angular 2和TypeScript的新手,我正在尝试遵循最佳实践.
我试图创建一个TypeScript类,而不是使用简单的JavaScript模型({}).
但是,Angular 2似乎并不喜欢它.
我的代码是:
import { Component, Input } from "@angular/core";
@Component({
selector: "testWidget",
template: "<div>This is a test and {{model.param1}} is my param.</div>"
})
export class testWidget {
constructor(private model: Model) {}
}
class Model {
param1: string;
}
Run Code Online (Sandbox Code Playgroud)
我用它作为:
import { testWidget} from "lib/testWidget";
@Component({
selector: "myComponent",
template: "<testWidget></testWidget>",
directives: [testWidget]
})
Run Code Online (Sandbox Code Playgroud)
我从Angular收到错误:
EXCEPTION:无法解析testWidget的所有参数:(?).
所以我想,Model还没有定义......我会把它移到顶端!
除了现在我得到例外:
原始例外:没有Model的提供者!
我怎么做到这一点?
编辑:感谢大家的回答.它让我走上了正确的道路.
为了将它注入构造函数,我需要将它添加到组件的提供者.
这似乎有效:
import { Component, Input } from "@angular/core";
class Model {
param1: string;
}
@Component({
selector: …
Run Code Online (Sandbox Code Playgroud) 我有以下小提琴:
http://jsfiddle.net/q05n5v4c/2/
在Chrome中,它渲染得很好.人字形在右侧.
但是,在Firefox中,它将雪佛龙下降了1行.
我搜索过谷歌并发现了几个关于这个bug的帖子,但没有一个建议有所帮助.
那里的任何CSS专家可以告诉我我做错了什么?
HTML:
<div class="btn-group">
<button data-toggle="dropdown"
class="btn btn-default dropdown-toggle"
style="width: 400px;text-align: left;">
Checked option
<span class="glyphicon glyphicon-chevron-down"
style='float: right;'></span>
</button>
</div>
Run Code Online (Sandbox Code Playgroud) 我们正在公司进行一些哲学辩论,讨论业务逻辑的调用应该在哪里执行CRUD操作.
我相信模型应该包含您的数据结构,并且控制器应该负责填充数据.
我的同事认为所有人口都应该在模型类中完成,并且只需要由控制器调用.这使控制器整洁干净(但在我看来,这会使模型变得混乱).
他还认为,任何返回Json对象的调用都应该在模型中进行,而不是在控制器中进行.该模型将向控制器返回一个数组,然后将其作为Json对象返回.
每个人有什么不同的利弊,是否有正确或错误的方法来做到这一点?
我试图Json.Encode(...)
在MVC 4中调用一个方法,我收到一个错误:The name 'Json' does not exist in the current context.
我已经尝试添加System.Runtime.Serialization
名称空间,它有Json
类,但这个特定的Json
类实例没有Encode
方法.
我在这里缺少什么装配?
这是我的确切代码:
{ url: '@Url.Action("DeleteDefinitionRule")', editData:
{ companyCode: 'LO', definitionID:@Html.Raw(Json.Encode(Model.DefinitionID)) } }
Run Code Online (Sandbox Code Playgroud) 我通过Microsoft CDN安装了Bootstrap,如:
<head>
...
<link href="//ajax.aspnetcdn.com/ajax/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="//ajax.aspnetcdn.com/ajax/bootstrap/3.1.1/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css" />
</head>
Run Code Online (Sandbox Code Playgroud)
和
<body>
...
<script src="//ajax.aspnetcdn.com/ajax/bootstrap/3.1.1/bootstrap.min.js"></script>
</body>
Run Code Online (Sandbox Code Playgroud)
使用Bootstrap一切正常,但Visual Studio并没有给我任何类型的智能感知.
例如,输入
<div class="(intellisense should open here)
Run Code Online (Sandbox Code Playgroud)
什么都没有出现.
有没有办法从CDN获得智能感知?
我正在尝试使用bootstrap variables.less文件处理一些网格宽度,但我想使用SASS而不是LESS.
我有以下代码:
jqGrid.scss
@import url('../../Content/bootstrap/bootstrap-ui/less/variables.less');
@mixin colWidthPercent($columnNum, $widthPercent) {
width: @screen-sm-min;
}
Run Code Online (Sandbox Code Playgroud)
它不喜欢@ screen-sm-min,我假设因为它是一种LESS格式.
有没有办法混合.less文件和.sass文件?
我们当前的PostgreSQL数据库使用GUID作为主键并将它们存储为Text字段.
我对此的初步反应是,尝试执行任何类型的最小笛卡尔联接都是索引的噩梦,试图找到所有匹配的记录.但是,也许我对数据库索引的有限理解在这里是错误的.
我认为我们应该使用UUID,因为它们存储为GUID的二进制表示,其中Text不是,并且您在Text列上获得的索引量是最小的.
改变这些将是一个重要的项目,我想知道它是否值得吗?
我有以下的plunker:
http://plnkr.co/edit/7YUpQ1tEjnUaX01txFcK?p=preview
当我运行它时,在范围中未定义templateUrl.为什么?
我在这里的假设是,它试图在父作用域中找到名为template.html的变量,但不能,因此它将它分配给undefined.如果是这样,我如何将其作为字符串而不是范围变量传递?
HTML:
<body ng-app="myApp">
<div ng-controller="TestCtrl">
<test-directive ng-model="testModel"
template-url="template.html">
</test-directive>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
.js文件
var app = angular.module("myApp", []);
app.controller("TestCtrl", function($scope) {
$scope.testModel = {}
});
app.directive("testDirective", function () {
return {
restrict: 'E',
scope: {
model: "=ngModel",
templateUrl: "="
},
template: "<div ng-include='templateUrl'></div>",
link: function (scope, element, attrs) {
console.log(scope.templateUrl); // <-- Shows as undefined
}
}
});
Run Code Online (Sandbox Code Playgroud) angularjs ×2
css ×2
angular ×1
c# ×1
cdn ×1
class ×1
css-float ×1
firefox ×1
intellisense ×1
json ×1
less ×1
owin ×1
postgresql ×1
primary-key ×1
sass ×1
typescript ×1
uuid ×1