小编sat*_*hvj的帖子

AngularJS从控制器触发并监视对象值的服务变化

我正试图从控制器中观察服务的变化.我在stackoverflow上尝试了很多基于很多qns的东西,但我一直无法使它工作.

HTML:

<div ng-app="myApp">
    <div ng-controller="MyCtrl">
        <div ng-click="setFTag()">Click Me</div>
    </div> 
</div>
Run Code Online (Sandbox Code Playgroud)

JavaScript的:

var myApp = angular.module('myApp',[]);

myApp.service('myService', function() {
    this.tags = {
        a: true,
        b: true
    };


    this.setFalseTag = function() {
        alert("Within myService->setFalseTag");
        this.tags.a = false;
        this.tags.b = false;

        //how do I get the watch in MyCtrl to be triggered?
    };
});


myApp.controller('MyCtrl', function($scope, myService) {

    $scope.setFTag = function() {
        alert("Within MyCtrl->setFTag");
        myService.setFalseTag();
    };        

    $scope.$watch(myService.tags, function(newVal, oldVal) {
        alert("Inside watch");
        console.log(newVal);
        console.log(oldVal);
    }, true);

});
Run Code Online (Sandbox Code Playgroud)

如何让手表在控制器中触发?

的jsfiddle

watch angularjs

35
推荐指数
1
解决办法
5万
查看次数

golang gorm和mysql:如何检索最后添加项的实例

我正在使用github.com/jinzhu/gorm和一个mysql后端.我想检索上一个Create调用中该行的Id(或完整实体).

如,last-insert-id:(http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id)

我怎样才能做到这一点?

mysql go last-insert-id

6
推荐指数
1
解决办法
6152
查看次数

Atom Editor:每个项目或打开的窗口的主题/语法

我想在 Atom 编辑器中为我的不同项目设置多个 UI 主题和语法主题,即每个打开的窗口都有一个主题 我该怎么做?目前,在首选项中更改一个将更改所有打开的窗口。

我安装了 project-manager 插件,并尝试在 ~/.atom/projects.cson 文件中添加一个条目,如下所示,但似乎没有效果。

project1:
  title: "project1"
  paths: [
    "/Users/abcd/coding/trials/project1"
  ]
  themes: [
    "atom-dark-ui"
    "monokai"
  ]
Run Code Online (Sandbox Code Playgroud)

还有什么我应该尝试的吗?

themes atom-editor

6
推荐指数
1
解决办法
993
查看次数

将数据存储区实体放入Go中的接口

我有几个具有相同字段ID的数据存储区.是否有可能创建一个可以获得此值的通用函数?有类似的东西吗?

var i interface{}
err = datastore.Get(c, key, &i)
v := reflect.ValueOf(i)
id := v.FieldByName("Id").String()
Run Code Online (Sandbox Code Playgroud)

上面的代码实际上给了我一个"数据存储:无效的实体类型"错误.

go google-cloud-datastore

4
推荐指数
1
解决办法
1725
查看次数

angularjs $ resource将查询资源列表转换为对象数组

我正在尝试从返回$ resource查询创建一个对象数组,如此SO问题所示:链接.但是,我不断获得相同的资源和其他元素列表.我有一个插件:这里 (您必须打开开发人员控制台才能看到输出.)

var app = angular.module('plunker', ['ngResource']);


app.factory('NameResource', function($resource) {

  var url = 'data.json';
  var res = $resource(url, null, {

    query: {
      method: 'GET',
      isArray: true,
      transformResponse: function(data, headersGetter) {
        var items = angular.fromJson(data);
        var models = [];
        angular.forEach(items, function(item) {
          models.push(item);
        });
        console.log("models: ", models);
        return models;
      }
    }
  });

  return res;
});

app.controller('MainCtrl', function($scope, NameResource) {
  $scope.names = NameResource.query();
  console.log('Inside controller: ', $scope.names);

  setTimeout(function(){console.log('after some time names is:', $scope.names)}, 3000);
});
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?或者我误解了什么.这两者有什么区别?它对我来说似乎非常相似.什么时候会引起问题?

javascript arrays rest angularjs

2
推荐指数
1
解决办法
3535
查看次数

golang sort.Sort随机输出并且是错误的

我有一个应用于结构的自定义排序功能.完整的代码在play.golang.org上.

type Stmt struct {
    Name  string
    After []string
}

func sortStmts(stmts []Stmt) []Stmt {
    sort.Sort(ByAfter(stmts))
    return stmts
}

type ByAfter []Stmt

func (a ByAfter) Len() int      { return len(a) }
func (a ByAfter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAfter) Less(i, j int) bool {
    isLess := true

    //fmt.Printf("%s.%+v is being compared with %s.%+v\n", a[i].Name, a[i].After, a[j].Name, a[j].After)

    for _, v := range a[i].After {
        if a[j].Name == v {
            isLess …
Run Code Online (Sandbox Code Playgroud)

sorting go

0
推荐指数
1
解决办法
440
查看次数